Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796544843; b=uao9OstAcdDkPlrgdgqfDbAeW9YpA6aLcK1VWza9t/a/qLS+7Ov9oR4G60rAWzSmbVQc cByHxf4ihAgQLzG7dR2lf1M93b5zUY0DU6WA6jxZIwd0BMpyHJZ3ZpRCVbeeon/3UBT+1 7sYnokinDPiycmyeaDRFub1IwKGVO8AKwo6dth4CuM0tLOMmXzulsCnlA5fMKd1uL7c1s Tw0lrfO2ueU73V9wukdinmoQblQCfsh2Fu96t/2Ggzv76PuF8VDAus/bAkSOlcZyVzJsK WuXtCiOaNOXbdMvKpOfiCiPFGunXdCXUZkHl7ABV4sVSK+Wt4WOXCTgTvvs/w4cFgVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796544843; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=N3ZfXorcRBDniK5cexVx3O+uFQs/thHzaWZoR/OWxwM=; b=SG36eArvpDjkth5epR3W81Si+vghfKCk3KLCF8OAKH1cyzuciX/dfn2WON4eGjQzPu6E YnRWAOBaHT9Tv5+Fjjg4FJQrR2tAbIRNCNVzz6UXi1AhYJkqz1ZxvsHnwzhTuotkcdWR9 12zkLmmCaVL+6PKqIds5iOXm61At6OCJYQ0sYG5/jMvPAnj3dlcpdtarGiqtlSESI3sKB Tamda/WUDeTzGz4gWgmEYA6H8upo6BeY2IVqP2dD1gFAY7xQ9tEPO3NZlq0rWzRmh9XVZ dyHyeqTPo3Ut/PqnNTr2bcwZIAUYlm+l+YCfiatxsRF+I9LE/HIF+igF2oZby+Ne+4g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=TlR+rvbK; arc=none smtp.remote-ip=209.85.214.175 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C15A2606A31F for ; Sun, 6 Dec 2026 08:14:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C15A2606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=TlR+rvbK Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2f020d0169eso11708515ad.0 for ; Sun, 06 Dec 2026 00:14:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1796544842; x=1797149642; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=N3ZfXorcRBDniK5cexVx3O+uFQs/thHzaWZoR/OWxwM=; b=TlR+rvbKIVkv5q0fGho5YC/lfw5hfBlIw17yE2YTbvD7SrIy+Rp7lUAY7QhhK0XafJ nf+ILZojLhiHVKx6L9GB+5QyljUbsTzWzQYa9gScnH7CcvuEMmxfZBBswUxQ1S+e6TM+ ahJm4WLgrtSs9LDBtHy9UIOkGKZ3hcA0iywVEeH8fk2Azr9drJJJrtYPGzAqhrC0PVE/ AZ/C8X2lrVMRb+H+XPwaNIu5e/uyhj85CilWrv2sNvmq3b/6raH3wBa75Mkj5dhPziu7 gf3zoszh7vsP85lw0VR+wYb86cN5vzTx/kGg16X6HtrQ+lACiYzXSmAiUl+4JkpH55vN okJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796544842; x=1797149642; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=N3ZfXorcRBDniK5cexVx3O+uFQs/thHzaWZoR/OWxwM=; b=HywnFfBUjo7JuJWguJY3kbgTh/DIYYKUi2aSYyUHKqziT6OVnRnKWR+SzZFJul2gtE wR02z4wKvSivZpUhf/qF0a0+zzCdyxGT1/q5rjI6Ih4FIUTU3PMK0+cyxLnJqgCzAsrM iWufXiVTUtP8jksa9B7CC33pvTCNIvXPoRr/noq33vq/HEOt5+/HslxGQJbpR0kjnNDa NxHzpAdfG2/cejHTcStjVcVSJ/fRNzsb7ImXmtd7dXXdQXqsC6YN/9ek9dH9fOwUrsOj LVWGzw5dsPv/G7y6X1RooNkHJQBNy49E7rEF7laeQdIuLSiuf8+yvc9tfg+7aVi6Wx77 6O9g== X-Forwarded-Encrypted: i=1; AL+hErFn0kSIgwua9+HVr0roT8TwED7V1dmtYOi4V/jeP1fBEE12cBbWBFAxLNM96ogo9YFiwDMfTSCA5OA=@lists.linux.dev X-Gm-Message-State: AB4q+uGpj3zfeGo9PPtkWubpxLebIaDamAHkw3v0ujEsYlATqnanU14V 4Q75a0PtsoOL6cKRrUe93ZwTOF3cMYuQuRD/AyI2gwFtzJMGh3Hy6tZLi9v6DeV6rtQ= X-Gm-Gg: AZFdTZdbx7Py0onaHnK/qRi5+1guc8uUTIqn5NdnesWrJoZo/C7scN3FMeKGsi06E3r RIYfLGfyp//wnHlR4hhY2ap2TNyIclyz2OYJ2P5iWxmRE95/LNoMv5615kN3JGuOaoS2iogNes2 KaJZP89Xtm2bduAaQuf1yDdN6M8DUnhHNqAz5tqrSuP0T23Bj9yO5O8EOvekD8aV0hOv0VOWjn8 DjMOdxKnXbh4Dv4dVqtYH93YQQ+cJYI9/fIjdK5XFFyv6wDiQTvjgfMAJKVOpbceLWiGLu1jacq Bt0JznUisZO7sVjIWVX6r1ymzsH7+d0oSyGJBj4tcBlyqziI+rniBXXyefKLp55DJv3tnrCBe9f iBLSdGKYzpbZONDacnH1LiXSLG2hB+slnWLKA7KJdHXmgsgOfBAQeDODSpdkR5lKFdWfZ0gOF4J GDGWPq3JxTFQt61D10pEQCGR6UYzZrHilkiBI48YdLVEnaEYXGmSSRKKIxZzmgPeI+bupnN7M1k X2f0g9aj+QuFwJsYbOt46sHbQZ8FA838SEPaUQtXI8Y0d9ZyDl7tbs73BMTeU7j/bHN6/GdDfUb RiiPqYmvlIBfi6pnajtn3pWwPbJrQ7RVRk0qSaTXnFpFE1bQ4GiW8UY= X-Received: by 2002:a17:902:d507:b0:2f0:7f99:e46b with SMTP id d9443c01a7336-2f1ba1f3069mr70245265ad.47.1796544841905; Sun, 06 Dec 2026 00:14:01 -0800 (PST) Received: from G6L4RL2QG9 ([2409:8a00:1832:6900:b956:4650:9531:d810]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f1b791d29dsm33968655ad.37.2026.12.06.00.13.55 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 06 Dec 2026 00:14:01 -0800 (PST) From: Muchun Song To: Oscar Salvador , David Hildenbrand , Mike Rapoport , Andrew Morton Cc: Muchun Song , "Liam R . Howlett" , Vlastimil Babka , Michal Hocko , Miaohe Lin , Naoya Horiguchi , Lance Yang , Qi Zheng , linux-mm@kvack.org, loongarch@lists.linux.dev, Huacai Chen , Muchun Song Subject: [PATCH v2 3/3] mm/hugetlb_vmemmap: remove HPG_vmemmap_optimized Date: Sun, 6 Dec 2026 16:13:24 +0800 Message-ID: <20261206081324.8274-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261206081324.8274-1-songmuchun@bytedance.com> References: <20261206081324.8274-1-songmuchun@bytedance.com> X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit HPG_vmemmap_optimized duplicates the state recorded in the shared vmemmap backing page and must be updated on every mapping transition. Derive the state with folio_vmemmap_optimized() instead and remove the flag and its explicit updates to simplify the code. Signed-off-by: Muchun Song --- include/linux/hugetlb.h | 3 --- mm/hugetlb.c | 15 ++++++-------- mm/hugetlb_vmemmap.c | 43 +++++++---------------------------------- mm/memory-failure.c | 4 ++-- 4 files changed, 15 insertions(+), 50 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 14af4396de7e..98338392789f 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -612,7 +612,6 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr, * modifications require hugetlb_lock. * HPG_freed - Set when page is on the free lists. * Synchronization: hugetlb_lock held for examination and modification. - * HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed. * HPG_raw_hwp_unreliable - Set when the hugetlb page has a hwpoison sub-page * that is not tracked by raw_hwp_page list. */ @@ -621,7 +620,6 @@ enum hugetlb_page_flags { HPG_migratable, HPG_temporary, HPG_freed, - HPG_vmemmap_optimized, HPG_raw_hwp_unreliable, HPG_cma, __NR_HPAGEFLAGS, @@ -681,7 +679,6 @@ HPAGEFLAG(RestoreReserve, restore_reserve) HPAGEFLAG(Migratable, migratable) HPAGEFLAG(Temporary, temporary) HPAGEFLAG(Freed, freed) -HPAGEFLAG(VmemmapOptimized, vmemmap_optimized) HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable) HPAGEFLAG(Cma, cma) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a0f25fbb1a74..a664bcb50005 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1510,7 +1510,7 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio, * pages. Otherwise, someone (memory error handling) may try to write * to tail struct pages. */ - if (!folio_test_hugetlb_vmemmap_optimized(folio)) + if (!folio_vmemmap_optimized(folio)) __folio_clear_hugetlb(folio); h->nr_huge_pages--; @@ -1522,14 +1522,13 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, { int nid = folio_nid(folio); - VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio); + VM_BUG_ON_FOLIO(!folio_vmemmap_optimized(folio), folio); lockdep_assert_held(&hugetlb_lock); /* Callers must clear transient flags before returning to the pool. */ VM_WARN_ON_ONCE_FOLIO((unsigned long)folio->private & - ~(BIT(HPG_vmemmap_optimized) | - BIT(HPG_raw_hwp_unreliable) | + ~(BIT(HPG_raw_hwp_unreliable) | BIT(HPG_cma)), folio); INIT_LIST_HEAD(&folio->lru); @@ -1550,7 +1549,7 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, static void __update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio) { - bool clear_flag = folio_test_hugetlb_vmemmap_optimized(folio); + bool clear_flag = folio_vmemmap_optimized(folio); if (hstate_is_gigantic_no_runtime(h)) { /* Callers must filter gigantic_no_runtime upstream. */ @@ -1658,7 +1657,7 @@ static inline void flush_free_hpage_work(struct hstate *h) static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio, bool atomic) { - if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) { + if (!folio_vmemmap_optimized(folio) || !atomic) { __update_and_free_hugetlb_folio(h, folio); return; } @@ -3481,8 +3480,6 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid) memmap_init_compound_page(page, pfn, zone, huge_page_order(h)); init_new_hugetlb_folio(folio); - if (vmemmap_optimizable_order(pfn_to_section_compound_order(pfn))) - folio_set_hugetlb_vmemmap_optimized(folio); section_set_compound_order_range(pfn, nr_pages, 0); list_add(&folio->lru, &folio_list); @@ -4115,7 +4112,7 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, int i; bool cma; - if (folio_test_hugetlb_vmemmap_optimized(folio)) + if (folio_vmemmap_optimized(folio)) continue; cma = folio_test_hugetlb_cma(folio); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 96203d3eba91..199b157b8f2b 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -393,6 +393,9 @@ static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags) int ret, nid = folio_nid(folio); LIST_HEAD(vmemmap_pages); + if (!folio_vmemmap_optimized(folio)) + return 0; + start += VMEMMAP_OPTIMIZATION_SIZE; ret = alloc_vmemmap_page_list(&vmemmap_pages, (end - start) / PAGE_SIZE, nid, GFP_KERNEL); @@ -412,18 +415,8 @@ early_param("hugetlb_free_vmemmap", hugetlb_vmemmap_optimize_param); static int __hugetlb_vmemmap_restore_folio(struct folio *folio, unsigned long flags) { - int ret; - VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio); - - if (!folio_test_hugetlb_vmemmap_optimized(folio)) - return 0; - - ret = vmemmap_restore_folio(folio, flags); - if (!ret) - folio_clear_hugetlb_vmemmap_optimized(folio); - - return ret; + return vmemmap_restore_folio(folio, flags); } /** @@ -461,7 +454,7 @@ long hugetlb_vmemmap_restore_folios(struct list_head *folio_list, unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH; list_for_each_entry_safe(folio, t_folio, folio_list, lru) { - if (folio_test_hugetlb_vmemmap_optimized(folio)) { + if (folio_vmemmap_optimized(folio)) { ret = __hugetlb_vmemmap_restore_folio(folio, flags); if (ret) break; @@ -483,37 +476,15 @@ static int __hugetlb_vmemmap_optimize_folio(struct folio *folio, struct list_head *vmemmap_pages, unsigned long flags) { - int ret; - VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio); - - /* - * Very Subtle - * If VMEMMAP_DEFER_TLB_FLUSH is set, TLB flushing is not performed - * immediately after remapping. As a result, subsequent accesses - * and modifications to struct pages associated with the hugetlb - * page could be to the OLD struct pages. Set the vmemmap optimized - * flag here so that it is copied to the new head page. This keeps - * the old and new struct pages in sync. - * If there is an error during optimization, we will immediately FLUSH - * the TLB and clear the flag below. - */ - folio_set_hugetlb_vmemmap_optimized(folio); - ret = vmemmap_optimize_folio(folio, vmemmap_pages, flags); - if (ret) - folio_clear_hugetlb_vmemmap_optimized(folio); - - return ret; + return vmemmap_optimize_folio(folio, vmemmap_pages, flags); } /** * hugetlb_vmemmap_optimize_folio - optimize @folio's vmemmap pages. * @folio: the folio whose vmemmap pages will be optimized. * - * This function only tries to optimize @folio's vmemmap pages and does not - * guarantee that the optimization will succeed after it returns. The caller - * can use folio_test_hugetlb_vmemmap_optimized(@folio) to detect if @folio's - * vmemmap pages have been optimized. + * On success, folio_vmemmap_optimized(@folio) returns true. */ void hugetlb_vmemmap_optimize_folio(struct folio *folio) { diff --git a/mm/memory-failure.c b/mm/memory-failure.c index a45e0cfcb9e9..2be5b4d4057a 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -2028,7 +2028,7 @@ static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag) * hugetlb_vmemmap_optimized hugepages can't be freed because struct * pages for tail pages are required but they don't exist. */ - if (move_flag && folio_test_hugetlb_vmemmap_optimized(folio)) + if (move_flag && folio_vmemmap_optimized(folio)) return 0; /* @@ -2045,7 +2045,7 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio) { if (folio_test_hugetlb_raw_hwp_unreliable(folio)) return; - if (folio_test_hugetlb_vmemmap_optimized(folio)) + if (folio_vmemmap_optimized(folio)) return; folio_clear_hwpoison(folio); folio_free_raw_hwp(folio, true); -- 2.54.0