From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (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 1FA0733F582 for ; Tue, 4 Aug 2026 03:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785815759; cv=none; b=rLkAn15Em1p78sJtLXwyNA47X8xAYBwd2rGFaZO6XbBKvkz/9+tpC1c/BnnSW/U/nZFwbzZx91zmYg+9j7AnMhYNWGQBzAMmK9vmEZ18sknMnjHm+lgSQGoV8SA50a9L+KuUqjAx1lItPgvDUfHOlhbNDv5iQqddAZChtakxa5Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785815759; c=relaxed/simple; bh=UWIqLaFuB5ADylvK38yUsqmlDhJ6djahCGnbGC2YYAo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MD08Tnj3C+4pMyKLSDXgPVJXEG6+OJzAcdX7b0988lok4nP5D1/qC2a+mbuQDkxPJ7VM/am1zazpoiJ3vspxnaTERRnlrfAwSkYeFehhXf7LBX0ErM0DBputykn2NRM7RNHjBqtf0uzdFsLrJ/4ejukExo5NRTBeLTs53NJR5LM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=bZgTcPy0; arc=none smtp.client-ip=209.85.215.169 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.b="bZgTcPy0" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-cbb85186d43so1964537a12.3 for ; Mon, 03 Aug 2026 20:55:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1785815757; x=1786420557; darn=vger.kernel.org; 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=c7rwKsso5Cym2RVCEFeMn64q7fu6QVT+bsIVBVphtqw=; b=bZgTcPy0ix4bTVYfkC80PS3rgXc6n3VWD0f9CciMaEprTPGFZJBvtjU+WPDjf2MGsu dpltGJZb92faDVMVcwCwRdu2KzTYktoXSUlowuJrtiP1W8JS1FjeQ9UC9pnDwq1xaOhG oRwmFGu2WSBQ08xhP0XS+EEVNTrwc5fHYxtQ74+DipTL1MbJnPoqyvWbKQ6bS4bL2asL Uc2eSzndh3SWL8s619poU3ihrb52FAHSbvWVAQyDQsLkoRH0yVcKwK0Xxc/BbfcjZTzq X4oO5bIBcFEyuUWGSQ8eXmd2DzpY4UcpKym2m1Gtx1r/l/YT5a5aCdcCfT1HOnprxCP+ Jt5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785815757; x=1786420557; 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=c7rwKsso5Cym2RVCEFeMn64q7fu6QVT+bsIVBVphtqw=; b=UNteug5fkZchrJ9oIz40ywvx1m2SLmxdtXr3ngWjdZA6XljCkVESq2KIdxCZIlN1hf hUQFmH3INo/oZ5ICeOKhUEh589F4WnCabXA10cv17hQW3RLwXFYB0oVxqIA3zjwVX8bA /7DMLWPxe0oqjy5cW2h5W+SdIcso4H43gHSMNTx9jtsdlIrALkTGExgvd3SWRFihUWdB ggiQlgKU5sm5p8S9j7rS50rtFTSrFjA+mbx9yM4Mvhv0ZwlquEpya0AMMALXVgd0HMBQ 7RqKrTETozcIheZMTVxUoOB31Jiv961IQo+dVLkoTN+ae3qs7J6EX5ddCVwzQg6cT1Pq Sb3w== X-Forwarded-Encrypted: i=1; AHgh+RouyQf6QoOg/VjCl+Zm6wwrt70z1crdPZ5X2F4x3TmU3hDFCrStcjFXubiOjlZdl3XzWVHuslOmX0M0Cms=@vger.kernel.org X-Gm-Message-State: AOJu0YzoQlaqT/9PfmcHCS6fgCD1gX9UvZirnkex1dmXdbBw6nrFMQkr P1kbE0sdTYczifjLw+fwohVl/CSodf1oLJFpkjNTZe7ruR0DEhgf5lufEPfCrDgHSVY= X-Gm-Gg: AR+sD13XMqmgqYuxufjQG/u63wiOVZDrgLdKDXb94LbU+hrubFuW2Eprz4bC//llw8+ 2TSKRxEVIYt3YODjz+Bv/hYrQWQ4BKONQrTySNZIbDbYXtjVm4eF19DJgAhHFdzqKi8LNibRQzU bEwNAOYYUpv1+o6jFQLBbedx3ckX1SccsCTX5abuXzjDPwV4sCUKCIl0GfScJ6Ne1N37WX8k2or Ko6aJ8ko7j+sdFId9W7cgbW83045OgRV/+AjybxWtXAmMK/7MmEhRf4LVM/hmEEfmvFeeQfh+j+ 3ME/FAvOOvmxXlqZo4apq1TJV5C+NZm0fqUGznTBTqEvPOiVYZcXqQZVikPzledZ6O1wh28poHk Hu/g/EDQleO6DodWmy8ROh3PWFSkMtmpkjJsiCIEohkZGSCO8GC0ideeg+C8Jlqh7Ifda3JAJnc fDu/vZJCAm0MQIf/2M05RcLA0mRVyAHQVd6TfjlX52RnL+cgGvqX23orIucfP++LoSYCH2CIa88 a9XN6yapA== X-Received: by 2002:a17:903:2f50:b0:2c9:b8b7:5d1c with SMTP id d9443c01a7336-2d0521b7f1emr128708045ad.16.1785815757210; Mon, 03 Aug 2026 20:55:57 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.99]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d04ae99af1sm46313695ad.33.2026.08.03.20.55.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 20:55:55 -0700 (PDT) From: Muchun Song To: Andrew Morton , Oscar Salvador , David Hildenbrand Cc: Mike Rapoport , Vlastimil Babka , Lorenzo Stoakes , Michal Hocko , David Laight , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Muchun Song Subject: [PATCH v3 03/17] mm/mm_init: skip initializing shared vmemmap tail pages Date: Tue, 4 Aug 2026 11:55:21 +0800 Message-ID: <20260804035535.2846016-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260804035535.2846016-1-songmuchun@bytedance.com> References: <20260804035535.2846016-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit memmap_init_range() initializes every struct page in the target range. For compound pages with vmemmap optimization, the tail struct pages are backed by a shared vmemmap page. Initializing those tail struct pages would overwrite the shared vmemmap page contents, requiring users such as HugeTLB to restore the metadata afterwards. Track the compound order for HVO-backed sections and use that metadata to detect struct pages that fall into the shared tail vmemmap range. Skip those shared tail pages in memmap_init_range(), then initialize pageblock migratetypes for the processed range with a helper after the per-page initialization loop. Keep direct mem_section access inside sparse helpers by exposing pfn_to_section_order() to users that only need the order associated with a PFN. This lets memmap_init_range() skip shared tail vmemmap pages without exposing __pfn_to_section() to !SPARSEMEM builds. This is a preparatory change for consolidating handling across users of vmemmap optimization, and it also avoids redundant initialization of shared tail vmemmap pages during early boot. Signed-off-by: Muchun Song --- v3: - Replace the !SPARSEMEM __pfn_to_section() stub with pfn_to_section_order() (suggested by Mike Rapoport) v2: - Fold section order tracking into the first user instead of keeping a standalone API-only patch (suggested by Mike Rapoport) - Rename page_vmemmap_optimizable() to pfn_vmemmap_optimizable() and pass a PFN directly (suggested by Mike Rapoport) - Initialize pageblock migratetypes from a helper after the per-page loop (suggested by Mike Rapoport) - Use a 1G PFN chunk for cond_resched() in the pageblock helper (suggested by Mike Rapoport) - Guard section_order() with CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP so it returns 0 when HVO is disabled and lets the compiler optimize the code as much as possible (suggested by Mike Rapoport) - Explain why the !SPARSEMEM __pfn_to_section() stub belongs here (suggested by Mike Rapoport) --- include/linux/mmzone.h | 8 ++++++++ mm/mm_init.c | 34 +++++++++++++++++----------------- mm/sparse.h | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index f96a45ac558f..81e16d71e1f0 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2023,6 +2023,14 @@ struct mem_section { unsigned long section_mem_map; struct mem_section_usage *usage; +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP + /* + * Normally, sections hold regular (order-0) pages. However, for + * sections with HVO enabled, this tracks the compound page order + * to enable deduplication of redundant vmemmap pages. + */ + unsigned int order; +#endif #ifdef CONFIG_PAGE_EXTENSION /* * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use diff --git a/mm/mm_init.c b/mm/mm_init.c index e5aa20a9b898..1823381a69bb 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -677,21 +678,19 @@ static inline void fixup_hashdist(void) static inline void fixup_hashdist(void) {} #endif /* CONFIG_NUMA */ -#if defined(CONFIG_ZONE_DEVICE) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT) static __meminit void pageblock_migratetype_init_range(unsigned long pfn, - unsigned long nr_pages, int migratetype, bool atomic) + unsigned long nr_pages, int migratetype, bool isolate, bool atomic) { const unsigned long end = pfn + nr_pages; for (pfn = pageblock_align(pfn); pfn < end; pfn += pageblock_nr_pages) { enum migratetype mt = kho_scratch_migratetype(pfn, migratetype); - init_pageblock_migratetype(pfn_to_page(pfn), mt, false); - if (!atomic && IS_ALIGNED(pfn, PAGES_PER_SECTION)) + init_pageblock_migratetype(pfn_to_page(pfn), mt, isolate); + if (!atomic && IS_ALIGNED(pfn, PFN_DOWN(SZ_1G))) cond_resched(); } } -#endif #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT static inline void pgdat_set_deferred_range(pg_data_t *pgdat) @@ -886,6 +885,13 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone } } + if (pfn_vmemmap_optimizable(pfn)) { + unsigned int order = pfn_to_section_order(pfn); + + pfn = min(ALIGN(pfn, 1UL << order), end_pfn); + continue; + } + page = pfn_to_page(pfn); __init_single_page(page, pfn, zone, nid); if (context == MEMINIT_HOTPLUG) { @@ -897,19 +903,13 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone __SetPageOffline(page); } - /* - * Usually, we want to mark the pageblock MIGRATE_MOVABLE, - * such that unmovable allocations won't be scattered all - * over the place during system boot. - */ - if (pageblock_aligned(pfn)) { - enum migratetype mt = kho_scratch_migratetype(pfn, migratetype); - - init_pageblock_migratetype(page, mt, isolate_pageblock); + if (pageblock_aligned(pfn)) cond_resched(); - } pfn++; } + + pageblock_migratetype_init_range(start_pfn, pfn - start_pfn, migratetype, + isolate_pageblock, false); } static void __init memmap_init_zone_range(struct zone *zone, @@ -1112,7 +1112,7 @@ void __ref memmap_init_zone_device(struct zone *zone, compound_nr_pages(pfn, altmap, pgmap)); } - pageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE, false); + pageblock_migratetype_init_range(start_pfn, nr_pages, MIGRATE_MOVABLE, false, false); pr_debug("%s initialised %lu pages in %ums\n", __func__, nr_pages, jiffies_to_msecs(jiffies - start)); @@ -1922,7 +1922,7 @@ static void __init deferred_free_pages(unsigned long pfn, if (!nr_pages) return; - pageblock_migratetype_init_range(pfn, nr_pages, MIGRATE_MOVABLE, true); + pageblock_migratetype_init_range(pfn, nr_pages, MIGRATE_MOVABLE, false, true); page = pfn_to_page(pfn); diff --git a/mm/sparse.h b/mm/sparse.h index 95aa031213f2..b9b6b47e85ce 100644 --- a/mm/sparse.h +++ b/mm/sparse.h @@ -10,6 +10,39 @@ #include +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +static inline unsigned int section_order(const struct mem_section *section) +{ + return section->order; +} + +static inline unsigned int pfn_to_section_order(unsigned long pfn) +{ + return section_order(__pfn_to_section(pfn)); +} +#else +static inline unsigned int section_order(const struct mem_section *section) +{ + return 0; +} + +static inline unsigned int pfn_to_section_order(unsigned long pfn) +{ + return 0; +} +#endif + +static inline bool pfn_vmemmap_optimizable(unsigned long pfn) +{ + const unsigned int order = pfn_to_section_order(pfn); + const unsigned long nr_pages = 1UL << order; + + if (!is_power_of_2(sizeof(struct page))) + return false; + + return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES; +} + /* * mm/sparse.c */ -- 2.54.0