From nobody Sat Feb 7 15:11:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1718425C818; Mon, 2 Feb 2026 15:58:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770047895; cv=none; b=LMH/lPKScdYAkL+91BL+iIFE2hh9EF8jj8yKV4gYHFs/+/OLf/zRMYVrsMihlFLM1YfkCJmmPuX0beTyeK6ugt9FNYTRq50gYVC6O2Ac4toYN3jheHXMviGOStkGCQlDJ8uzUWyTO8XG1TuOnUvn9mSPCHL2HMs2TacOQQrwrEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770047895; c=relaxed/simple; bh=8150cyoGX9SS1fXE9plqym887h813uJpRn/Ewh7r9v8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WX9sK888F6rFgPcuy+xwtbJvJ2LmIiBtCTHYhr6CednjWYYjXsKuh8X9THh+gCY2xC6qJeemKaQ/y2rKnKXqLwwEcStCY15oTsEJ9FYW487Q9Rzifs+L9MHkK4utKvrAmXov9lV3qLqBIL9sjkZpyX75hZxxdQSfhq7LwLdIIPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pCTVTrn2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pCTVTrn2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F913C116C6; Mon, 2 Feb 2026 15:58:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770047894; bh=8150cyoGX9SS1fXE9plqym887h813uJpRn/Ewh7r9v8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pCTVTrn2xIfXEgDFZxJ7MNSokBBZtXjXd8VlKnhKCBCzTx8fZnHY4HyrN17XCH5F2 /kTY/xFUOm+7GUvGwvpA+ZLlPJ0UuNQctj+cZUYNbxl9LQ+wNhL8L7Pm64Y3ZRiWIw zClXFNqdvSrARA5RBgAc6nkgwiREJUW/5Ep2NvO2y7FKBGqMpum0Tk8qsFmtE3A9LE 9ES7+2doVIX89ZSYIVNNTuBkgACnEEeGmeghL3cTJzksK4tpzxJZW6JzuhaFjpBCbm CT2b1KrxAn/OVgL2YguYC081eZTzFh57KgVGWA9z0UmzyfiUvPw1WLw16LgFLvNOA4 dyANFCuX2yexg== Received: from phl-compute-08.internal (phl-compute-08.internal [10.202.2.48]) by mailfauth.phl.internal (Postfix) with ESMTP id 4456BF40069; Mon, 2 Feb 2026 10:58:13 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Mon, 02 Feb 2026 10:58:13 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddujeektdeiucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepmfhirhihlhcu ufhhuhhtshgvmhgruhcuoehkrghssehkvghrnhgvlhdrohhrgheqnecuggftrfgrthhtvg hrnhephfdufeejhefhkedtuedvfeevjeffvdfhvedtudfgudffjeefieekleehvdetvdev necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepkhhirh hilhhlodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdduieduudeivdeiheeh qddvkeeggeegjedvkedqkhgrsheppehkvghrnhgvlhdrohhrghesshhhuhhtvghmohhvrd hnrghmvgdpnhgspghrtghpthhtohepvdekpdhmohguvgepshhmthhpohhuthdprhgtphht thhopegrkhhpmheslhhinhhugidqfhhouhhnuggrthhiohhnrdhorhhgpdhrtghpthhtoh epmhhutghhuhhnrdhsohhngheslhhinhhugidruggvvhdprhgtphhtthhopegurghvihgu sehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhllhihsehinhhfrhgruggvrggurd horhhgpdhrtghpthhtohepuhhsrghmrggrrhhifheigedvsehgmhgrihhlrdgtohhmpdhr tghpthhtohepfhhvughlsehgohhoghhlvgdrtghomhdprhgtphhtthhopehoshgrlhhvrg guohhrsehsuhhsvgdruggvpdhrtghpthhtoheprhhpphhtsehkvghrnhgvlhdrohhrghdp rhgtphhtthhopehvsggrsghkrgesshhushgvrdgtii X-ME-Proxy: Feedback-ID: i10464835:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 2 Feb 2026 10:58:11 -0500 (EST) From: Kiryl Shutsemau To: Andrew Morton , Muchun Song , David Hildenbrand , Matthew Wilcox , Usama Arif , Frank van der Linden Cc: Oscar Salvador , Mike Rapoport , Vlastimil Babka , Lorenzo Stoakes , Zi Yan , Baoquan He , Michal Hocko , Johannes Weiner , Jonathan Corbet , Huacai Chen , WANG Xuerui , Palmer Dabbelt , Paul Walmsley , Albert Ou , Alexandre Ghiti , kernel-team@meta.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, Kiryl Shutsemau Subject: [PATCHv6 11/17] mm/hugetlb: Remove fake head pages Date: Mon, 2 Feb 2026 15:56:27 +0000 Message-ID: <20260202155634.650837-12-kas@kernel.org> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20260202155634.650837-1-kas@kernel.org> References: <20260202155634.650837-1-kas@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" HugeTLB Vmemmap Optimization (HVO) reduces memory usage by freeing most vmemmap pages for huge pages and remapping the freed range to a single page containing the struct page metadata. With the new mask-based compound_info encoding (for power-of-2 struct page sizes), all tail pages of the same order are now identical regardless of which compound page they belong to. This means the tail pages can be truly shared without fake heads. Allocate a single page of initialized tail struct pages per NUMA node per order in the vmemmap_tails[] array in pglist_data. All huge pages of that order on the node share this tail page, mapped read-only into their vmemmap. The head page remains unique per huge page. Redefine MAX_FOLIO_ORDER using ilog2(). The define has to produce a compile-constant as it is used to specify vmemmap_tail array size. For some reason, compiler is not able to solve get_order() at compile-time, but ilog2() works. Avoid PUD_ORDER to define MAX_FOLIO_ORDER as it adds dependency to which generates hard-to-break include loop. This eliminates fake heads while maintaining the same memory savings, and simplifies compound_head() by removing fake head detection. Signed-off-by: Kiryl Shutsemau Reviewed-by: Muchun Song --- include/linux/mmzone.h | 19 +++++++++++++++++-- mm/hugetlb_vmemmap.c | 34 +++++++++++++++++++++++++++++++-- mm/sparse-vmemmap.c | 43 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 192143b5cdc0..c01f8235743b 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -81,13 +81,17 @@ * currently expect (see CONFIG_HAVE_GIGANTIC_FOLIOS): with hugetlb, we ex= pect * no folios larger than 16 GiB on 64bit and 1 GiB on 32bit. */ -#define MAX_FOLIO_ORDER get_order(IS_ENABLED(CONFIG_64BIT) ? SZ_16G : SZ_= 1G) +#ifdef CONFIG_64BIT +#define MAX_FOLIO_ORDER (ilog2(SZ_16G) - PAGE_SHIFT) +#else +#define MAX_FOLIO_ORDER (ilog2(SZ_1G) - PAGE_SHIFT) +#endif #else /* * Without hugetlb, gigantic folios that are bigger than a single PUD are * currently impossible. */ -#define MAX_FOLIO_ORDER PUD_ORDER +#define MAX_FOLIO_ORDER (PUD_SHIFT - PAGE_SHIFT) #endif =20 #define MAX_FOLIO_NR_PAGES (1UL << MAX_FOLIO_ORDER) @@ -1402,6 +1406,14 @@ struct memory_failure_stats { }; #endif =20 +/* + * vmemmap optimization (like HVO) is only possible for page orders that f= ill + * two or more pages with struct pages. + */ +#define VMEMMAP_TAIL_MIN_ORDER (ilog2(2 * PAGE_SIZE / sizeof(struct page))) +#define __NR_VMEMMAP_TAILS (MAX_FOLIO_ORDER - VMEMMAP_TAIL_MIN_ORDER + 1) +#define NR_VMEMMAP_TAILS (__NR_VMEMMAP_TAILS > 0 ? __NR_VMEMMAP_TAILS : 0) + /* * On NUMA machines, each NUMA node would have a pg_data_t to describe * it's memory layout. On UMA machines there is a single pglist_data which @@ -1550,6 +1562,9 @@ typedef struct pglist_data { #ifdef CONFIG_MEMORY_FAILURE struct memory_failure_stats mf_stats; #endif +#ifdef CONFIG_SPARSEMEM_VMEMMAP + struct page *vmemmap_tails[NR_VMEMMAP_TAILS]; +#endif } pg_data_t; =20 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index a39a301e08b9..688764c52c72 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -19,6 +19,7 @@ =20 #include #include "hugetlb_vmemmap.h" +#include "internal.h" =20 /** * struct vmemmap_remap_walk - walk vmemmap page table @@ -505,6 +506,32 @@ static bool vmemmap_should_optimize_folio(const struct= hstate *h, struct folio * return true; } =20 +static struct page *vmemmap_get_tail(unsigned int order, int node) +{ + struct page *tail, *p; + unsigned int idx; + + idx =3D order - VMEMMAP_TAIL_MIN_ORDER; + tail =3D READ_ONCE(NODE_DATA(node)->vmemmap_tails[idx]); + if (tail) + return tail; + + tail =3D alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); + if (!tail) + return NULL; + + p =3D page_to_virt(tail); + for (int i =3D 0; i < PAGE_SIZE / sizeof(struct page); i++) + prep_compound_tail(p + i, NULL, order); + + if (cmpxchg(&NODE_DATA(node)->vmemmap_tails[idx], NULL, tail)) { + __free_page(tail); + tail =3D READ_ONCE(NODE_DATA(node)->vmemmap_tails[idx]); + } + + return tail; +} + static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio, struct list_head *vmemmap_pages, @@ -520,6 +547,11 @@ static int __hugetlb_vmemmap_optimize_folio(const stru= ct hstate *h, if (!vmemmap_should_optimize_folio(h, folio)) return ret; =20 + nid =3D folio_nid(folio); + vmemmap_tail =3D vmemmap_get_tail(h->order, nid); + if (!vmemmap_tail) + return -ENOMEM; + static_branch_inc(&hugetlb_optimize_vmemmap_key); =20 if (flags & VMEMMAP_SYNCHRONIZE_RCU) @@ -537,7 +569,6 @@ static int __hugetlb_vmemmap_optimize_folio(const struc= t hstate *h, */ folio_set_hugetlb_vmemmap_optimized(folio); =20 - nid =3D folio_nid(folio); vmemmap_head =3D alloc_pages_node(nid, GFP_KERNEL, 0); if (!vmemmap_head) { ret =3D -ENOMEM; @@ -548,7 +579,6 @@ static int __hugetlb_vmemmap_optimize_folio(const struc= t hstate *h, list_add(&vmemmap_head->lru, vmemmap_pages); memmap_pages_add(1); =20 - vmemmap_tail =3D vmemmap_head; vmemmap_start =3D (unsigned long)&folio->page; vmemmap_end =3D vmemmap_start + hugetlb_vmemmap_size(h); =20 diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 37522d6cb398..13bcf5562f1b 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -378,16 +378,44 @@ void vmemmap_wrprotect_hvo(unsigned long addr, unsign= ed long end, } } =20 -/* - * Populate vmemmap pages HVO-style. The first page contains the head - * page and needed tail pages, the other ones are mirrors of the first - * page. - */ +static __meminit unsigned long vmemmap_get_tail(unsigned int order, int no= de) +{ + struct page *p, *tail; + unsigned int idx; + + BUG_ON(order < VMEMMAP_TAIL_MIN_ORDER); + BUG_ON(order > MAX_FOLIO_ORDER); + + idx =3D order - VMEMMAP_TAIL_MIN_ORDER; + tail =3D NODE_DATA(node)->vmemmap_tails[idx]; + if (tail) + return page_to_pfn(tail); + + p =3D vmemmap_alloc_block_zero(PAGE_SIZE, node); + if (!p) + return 0; + + for (int i =3D 0; i < PAGE_SIZE / sizeof(struct page); i++) + prep_compound_tail(p + i, NULL, order); + + tail =3D virt_to_page(p); + NODE_DATA(node)->vmemmap_tails[idx] =3D tail; + + return page_to_pfn(tail); +} + int __meminit vmemmap_populate_hvo(unsigned long addr, unsigned long end, int node, unsigned long headsize) { + unsigned long maddr, len, tail_pfn; + unsigned int order; pte_t *pte; - unsigned long maddr; + + len =3D end - addr; + order =3D ilog2(len * sizeof(struct page) / PAGE_SIZE); + tail_pfn =3D vmemmap_get_tail(order, node); + if (!tail_pfn) + return -ENOMEM; =20 for (maddr =3D addr; maddr < addr + headsize; maddr +=3D PAGE_SIZE) { pte =3D vmemmap_populate_address(maddr, node, NULL, -1, 0); @@ -398,8 +426,7 @@ int __meminit vmemmap_populate_hvo(unsigned long addr, = unsigned long end, /* * Reuse the last page struct page mapped above for the rest. */ - return vmemmap_populate_range(maddr, end, node, NULL, - pte_pfn(ptep_get(pte)), 0); + return vmemmap_populate_range(maddr, end, node, NULL, tail_pfn, 0); } =20 void __weak __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node, --=20 2.51.2