From mboxrd@z Thu Jan 1 00:00:00 1970 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 946823E3159 for ; Tue, 31 Mar 2026 20:07:18 +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=1774987638; cv=none; b=Hg522xPQVinTv/ujiHa/q+OfctALxo6ZbXZw7CMSEvwFg8ZQsT8TjNHi8thaHx0yH9TuSLLuj7WTZDOe1WeyR9d0RfASWHd8VqUv/0EpZ40qCJ3XyemClidBxKuey/1rxHg9YA3lxrC9HT/HFJGE+F9FqmuJEvX7YjlEsJO5gL4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774987638; c=relaxed/simple; bh=0zk0sqn0FkmeYVYUijfJAwUUjlYh2sei1OoJrZ+6nDc=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=GNUqQdV4zxFqfvTNaqgr+QOexFTFSd7V2GxE6S9k01+NF3WzGwHTVTZhInAekcYnTcFR51EoDv9h8yC8FR2CYqam8ddldRcwj+IVfosUmhfjkaWfC3pN+fDb1LBQH/wofPyK2JSxcX3/Ilvl32+TQuV7+DJltJYMs7i604rbuzo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=F5kDC2M0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="F5kDC2M0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9150C19423; Tue, 31 Mar 2026 20:07:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1774987638; bh=0zk0sqn0FkmeYVYUijfJAwUUjlYh2sei1OoJrZ+6nDc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=F5kDC2M0Zd2Q9Nll9hn1aqrLNVZ62oMtnKkzFd0rytJKKQD4fbDqggvhvxSfCA2NM IBz18GTZqNlMCvLICiMcXWvvyE4p2yCbmE5eXsuWCL1u5hsk17yhD+vba8olUSHCXN urnibr0i+tagxGXH4c7YroKsgZTJtEWZZ/lr8o0k= Date: Tue, 31 Mar 2026 13:07:17 -0700 From: Andrew Morton To: Muchun Song Cc: David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Petr Tesarik , linux-mm@kvack.org, linux-kernel@vger.kernel.org, muchun.song@linux.dev Subject: Re: [PATCH] mm/sparse: fix BUILD_BUG_ON check for section map alignment Message-Id: <20260331130717.d42b64e5179c4c814bc523ea@linux-foundation.org> In-Reply-To: <20260331113023.2068075-1-songmuchun@bytedance.com> References: <20260331113023.2068075-1-songmuchun@bytedance.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 31 Mar 2026 19:30:23 +0800 Muchun Song wrote: > The comment in mmzone.h states that the alignment requirement > is the minimum of PAGE_SHIFT and PFN_SECTION_SHIFT. However, the > pointer arithmetic (mem_map - section_nr_to_pfn()) results in > a byte offset scaled by sizeof(struct page). Thus, the actual > alignment provided by the second term is PFN_SECTION_SHIFT + > __ffs(sizeof(struct page)). > > Update the compile-time check and the mmzone.h comment to > accurately reflect this mathematically guaranteed alignment by > taking the minimum of PAGE_SHIFT and PFN_SECTION_SHIFT + > __ffs(sizeof(struct page)). This avoids the issue of the check > being overly restrictive on architectures like powerpc where > PFN_SECTION_SHIFT alone is very small (e.g., 6). > > Also, remove the exhaustive per-architecture bit-width list from the > comment; such details risk falling out of date over time and may > inadvertently be left un-updated, while the existing BUILD_BUG_ON > provides sufficient compile-time verification of the constraint. > > No runtime impact so far: SECTION_MAP_LAST_BIT happens to fit within > the smaller limit on all existing architectures. > > ... > > --- a/mm/sparse.c > +++ b/mm/sparse.c > @@ -269,7 +269,8 @@ static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long p > { > unsigned long coded_mem_map = > (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); > - BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT); > + BUILD_BUG_ON(SECTION_MAP_LAST_BIT > min(PFN_SECTION_SHIFT + __ffs(sizeof(struct page)), > + PAGE_SHIFT)); > BUG_ON(coded_mem_map & ~SECTION_MAP_MASK); > return coded_mem_map; > } In mm-stable this was moved into mm/internal.h's new sparse_init_one_section(). By David's 6a2f8fb8ed2d ("mm/sparse: move sparse_init_one_section() to internal.h") I did the obvious thing: include/linux/mmzone.h | 24 +++++++++--------------- mm/internal.h | 3 ++- 2 files changed, 11 insertions(+), 16 deletions(-) --- a/include/linux/mmzone.h~mm-sparse-fix-build_bug_on-check-for-section-map-alignment +++ a/include/linux/mmzone.h @@ -2068,21 +2068,15 @@ static inline struct mem_section *__nr_t extern size_t mem_section_usage_size(void); /* - * We use the lower bits of the mem_map pointer to store - * a little bit of information. The pointer is calculated - * as mem_map - section_nr_to_pfn(pnum). The result is - * aligned to the minimum alignment of the two values: - * 1. All mem_map arrays are page-aligned. - * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT - * lowest bits. PFN_SECTION_SHIFT is arch-specific - * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the - * worst combination is powerpc with 256k pages, - * which results in PFN_SECTION_SHIFT equal 6. - * To sum it up, at least 6 bits are available on all architectures. - * However, we can exceed 6 bits on some other architectures except - * powerpc (e.g. 15 bits are available on x86_64, 13 bits are available - * with the worst case of 64K pages on arm64) if we make sure the - * exceeded bit is not applicable to powerpc. + * We use the lower bits of the mem_map pointer to store a little bit of + * information. The pointer is calculated as mem_map - section_nr_to_pfn(). + * The result is aligned to the minimum alignment of the two values: + * + * 1. All mem_map arrays are page-aligned. + * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT lowest bits. Because + * it is subtracted from a struct page pointer, the offset is scaled by + * sizeof(struct page). This provides an alignment of PFN_SECTION_SHIFT + + * __ffs(sizeof(struct page)). */ enum { SECTION_MARKED_PRESENT_BIT, --- a/mm/internal.h~mm-sparse-fix-build_bug_on-check-for-section-map-alignment +++ a/mm/internal.h @@ -972,7 +972,8 @@ static inline void sparse_init_one_secti { unsigned long coded_mem_map; - BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT); + BUILD_BUG_ON(SECTION_MAP_LAST_BIT > min(PFN_SECTION_SHIFT + __ffs(sizeof(struct page)), + PAGE_SHIFT)); /* * We encode the start PFN of the section into the mem_map such that _ (boy that's an eyesore on an 80-col xterm!)