From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-185.mta1.migadu.com (out-185.mta1.migadu.com [95.215.58.185]) (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 95BC3203710 for ; Tue, 11 Aug 2026 03:46:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.185 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786419963; cv=none; b=DclD7uX/ULzxGjUXfsh++bH3IBJUDt/dyk/iU4Z6HEur/aCQcOha9Q0BUuYyLTn0S8S2NDV9g/xTJuvE1MYgBjahowxGcNnqygvbqgx+4uljHjj+cbxskrhJUJ1tbnrCKHltHOnmXoMzi5xtpDuO0Ab4RWNHkdN8X9QlIwZt46U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786419963; c=relaxed/simple; bh=VM2cXYv33mGGXhXc8HNs/Y8mlMxPy2zLvlrNdqNtTmo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=DqYbN8bQLmeN+DFMyGlX3aiC9hFjiyoYA7pOmruxGTUTydhZ3aNpnW9tpbTXHSGdTPJsUNwBeRqtehtHdD6899cuOvsNVNoMNT3zm7p1c5a00LkkDOUd+IhEiX4CyUEg8vBRmy9QvlBkEAPVeuOemmMemG7BFwEUv354UBwhm5Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=M7pn/2zP; arc=none smtp.client-ip=95.215.58.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="M7pn/2zP" Message-ID: <6069cc82-d215-476c-a02f-52e0ae181ea9@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786419959; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vf2wIzPBqAGNqktfjw6b1H1DhKoJGLNVcDHT9vkPtn8=; b=M7pn/2zP9FBtQrZbmPM8eY7ue4Je3wynbTkJc3I95bes1QEJMlohiT881QizgnubsigJ+X pKC3WND8cibDE6KCchW5Khj84HoEk60eRYKN/KfdRhkyCNhw8POjGTrlVUzyP0dx8iO+jX yORIzWl0+o7KpEFdCPS2pG0dsB967wo= Date: Tue, 11 Aug 2026 11:45:41 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH v3 05/17] mm/sparse-vmemmap: support section-based vmemmap accounting To: Muchun Song Cc: Mike Rapoport , Vlastimil Babka , Lorenzo Stoakes , Michal Hocko , David Laight , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Oscar Salvador , David Hildenbrand References: <20260804035535.2846016-1-songmuchun@bytedance.com> <20260804035535.2846016-6-songmuchun@bytedance.com> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: <20260804035535.2846016-6-songmuchun@bytedance.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2026/8/4 11:55, Muchun Song wrote: > section_nr_vmemmap_pages() can account ordinary sections and DAX sections, > but section-based vmemmap optimization keeps its compound order in struct > mem_section and retains a different number of vmemmap pages. > > Teach section_nr_vmemmap_pages() to recognize section-based optimized > sections and calculate their vmemmap page count from the section order > and the HVO retained page count. > > Signed-off-by: Muchun Song > --- > v3: > - Add vmemmap_optimizable_order() for order-based optimization checks > > v2: > - Remove an unnecessary vmemmap_can_optimize() call to simplify the code > (suggested by Mike Rapoport). > - Rewrite the commit message for better understanding. > --- > include/linux/mmzone.h | 6 ++++-- > mm/sparse-vmemmap.c | 10 ++++++---- > mm/sparse.h | 16 ++++++++++++++++ > 3 files changed, 26 insertions(+), 6 deletions(-) > > diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h > index 81e16d71e1f0..663726dd6463 100644 > --- a/include/linux/mmzone.h > +++ b/include/linux/mmzone.h > @@ -107,8 +107,10 @@ > is_power_of_2(sizeof(struct page)) ? \ > MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0) > > -/* The number of struct pages covered by the retained vmemmap pages with HVO enabled. */ > -#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES (PAGE_SIZE / sizeof(struct page)) > +/* The number of retained vmemmap pages with HVO enabled. */ > +#define VMEMMAP_OPTIMIZATION_PAGES 1 > +#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES \ > + (VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page)) > #define VMEMMAP_OPTIMIZATION_MIN_ORDER (ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1) > > #define __VMEMMAP_OPTIMIZATION_NR_ORDERS \ > diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c > index 107215cf8488..b7abc5494bb9 100644 > --- a/mm/sparse-vmemmap.c > +++ b/mm/sparse-vmemmap.c > @@ -649,24 +649,26 @@ void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn) > static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages, > struct vmem_altmap *altmap, struct dev_pagemap *pgmap) > { > - const unsigned int order = pgmap ? pgmap->vmemmap_shift : 0; > + const struct mem_section *ms = __pfn_to_section(pfn); > + const int order = pgmap ? pgmap->vmemmap_shift : section_order(ms); > + const int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES; > const unsigned long pages_per_compound = 1UL << order; > > VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION)); > VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION); > > - if (!vmemmap_can_optimize(altmap, pgmap)) > + if (!vmemmap_can_optimize(altmap, pgmap) && !section_vmemmap_optimizable(ms)) > return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE); Sashiko said this conditional logic mix DAX and HVO optimization checks in a way that severely corrupts vmemmap accounting for nr_memmap_pages. This is a false positive because its premise is based on memory hotplug support for HVO. However, HVO cannot be enabled through memory hotplug at this time. Muchun, Thanks. > > if (order < PFN_SECTION_SHIFT) { > VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound)); > - return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound; > + return vmemmap_pages * nr_pages / pages_per_compound; > } > > VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)); > > if (IS_ALIGNED(pfn, pages_per_compound)) > - return VMEMMAP_RESERVE_NR; > + return vmemmap_pages; > > return 0; > } > diff --git a/mm/sparse.h b/mm/sparse.h > index b9b6b47e85ce..6ad190ec48cf 100644 > --- a/mm/sparse.h > +++ b/mm/sparse.h > @@ -43,6 +43,17 @@ static inline bool pfn_vmemmap_optimizable(unsigned long pfn) > return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES; > } > > +static inline bool vmemmap_optimizable_order(unsigned int order) > +{ > + if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP)) > + return false; > + > + if (!is_power_of_2(sizeof(struct page))) > + return false; > + > + return order >= VMEMMAP_OPTIMIZATION_MIN_ORDER; > +} > + > /* > * mm/sparse.c > */ > @@ -80,6 +91,11 @@ static inline void __section_mark_present(struct mem_section *ms, > > ms->section_mem_map |= SECTION_MARKED_PRESENT; > } > + > +static inline bool section_vmemmap_optimizable(const struct mem_section *section) > +{ > + return vmemmap_optimizable_order(section_order(section)); > +} > #else > static inline void sparse_init(void) {} > #endif /* CONFIG_SPARSEMEM */