From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 C40B33E0758 for ; Tue, 25 Aug 2026 08:49:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787647748; cv=none; b=pwgRHMG8PEB0lfXF5TLkyNWik1jOk9/jxv2dAjkiYEAgGw8hWYhRgkkfbKg1GWl+vXlHG73Hd32jZk/+ZSRKXIP7X77nqiGDAIunIeOGcnFOVOCwV6WSFX1+Mu4ZJZ+SDL9xcFWI6Vt/64CG8vi5LoYrMcAFcmtCRUHzFIlQBiQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787647748; c=relaxed/simple; bh=fb22Klqi5RrupQKeoji8R+9SJ4vTRSBLUMnS/YArHeY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OV2gwdbLj/uu8C7NwrMhsUEo/BRSmwYt0XOI2ojRj5eTjPVnJcpL4SBV2W5jaIjx1bkpg7KRlI2xeP6TiKdMpQmw/8i7aQtflQFGJhsKcEtMuluYcFs+Ix0igSlT+ONtl1YWXiZ3ikGDFHAdAJvIF6eEoKIzKyztkWE609yTELU= 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=T5lzqA9o; arc=none smtp.client-ip=209.85.216.47 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="T5lzqA9o" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-3964e76d0f4so595898a91.3 for ; Tue, 25 Aug 2026 01:49:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1787647744; x=1788252544; 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=w63tlQxIXv6hFZNtC3XunmfoLQDKL59/Pnvl33aJ5Uo=; b=T5lzqA9owpMPDhw2MNtGHamWJyaAqNETOXkCSxL4Ixp44hFjGJXQKNmhmOjQhyAknf OXlz2QooXIx2/oSRKulEOs4m2DFj+uDv8YWzU2sC3FXr2yXFtFVnjsLsBJO4ilTJcF91 rTfLc6+pcMRwKaK19Ov7crht7AF+EDoMyYfHezml9eAyVruWXlZ2RgzMSeTz72JYqs/r cSZcbcC1wIcZ8b57gX10yos+mcQZFh+XdVhGwCQ4JX+7WuVBxQpbCjbWr0oY5ga2vF2b WvdjHMdiOB2XvUdxTVHuLtfvbeAggNrtVNcvQLKC2/R8bzFBu0NbUbPOydQXPqyDM+of jBGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787647744; x=1788252544; 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=w63tlQxIXv6hFZNtC3XunmfoLQDKL59/Pnvl33aJ5Uo=; b=G+4mRfPHR7qjLSoCW3CXoDVUjuH8eM4ReZ3fW9cU55z7G6TuQoJ3e6LbMRO09vbFVh 2LYb5j5nvfNqy/viiJw7sOE7wIFrNbONfpb0eETalW0qfdVcCerkQWwNqC3ALpIjB1hy PveaWMIX04oDWn1wRkdMEQA1cJwcNvTaASc5rA/GY9SqyfG/6K7vF8nG87Hye5pxkTkw kqOMGZloJucCLss5jzc9+S8gTs5FjZnQ/ZZ512yerp1Xb0QLPdsrkLYvgwup+xHVTfBM 5HJwGhJydlBny76NvvPNJg+vY8ccJSMPLg+SMuHCqPMHS91FIYSysklVVYyHOvGwuxSv hGQA== X-Forwarded-Encrypted: i=1; AHgh+Rrty+IYsl6b7mGnfFxm8THbjIFybHyUVasKx7lzZi+lYbJ2zf643wGd0gX1NsbCQACUGMgmV0Ui5AkKM+0=@vger.kernel.org X-Gm-Message-State: AFuF++ku6I6CkDQ2nz+IYoyPwlP96W/cA3hNbtPfbWoXyKU5crOgB6Ca YKKrskJwaNTmPUMgMxm0oYyD36FMebBVwatVABFgKS2uI6diDkQ2MEYaCJ0ps+XNhu8= X-Gm-Gg: AR+sD10YE+e3Iyq9FNkadIpIz8Rtg8Ge+luZqrCM5o/hrDxUAoZRPiiqcP66nJzQmSE GEsOK2O/XdlTqUZtWwRkMA20yFHpwsehg6MPUq0BaImq1TK3cgOw1oYn3rQ5yoebpFudF1Q7Wn/ q+15y7LwWbHtqIUPPxus2UnjOuC1zBrAbeivya045jxeYfTSk5fsnkg+dVqgqfA5Y0DClcPY5sR DADAIsp4v+lCBCqnQwXn6M7e5FD/Mg8CwzDFvmolcgqM5llsSqXIfWHja9wHpq+IJcKusYUK8t3 VF+8fIB8npGkPAItqqhkvD5cNYnmIpZQuvyd8Xmn9E0+fv8SeWD67uN1Dd9oC9lSh6qaAV8qrVV etxRYOU0Wc6u2/QgHFgsmVyy121BGzO0rAReN2vAGObEIvJifMMjXhHfi1vfGvA4eywiJzFma6r G7MiUN4IFNeRwefrfOLSiVTJiVl4fE+Nnngr5H5PToybqOHrun5sBYIQNMWZd8ipK4RnXa5+OVT Lx0ipZX/+alqvdV3Z/ZRl7sFwTVhDE= X-Received: by 2002:a17:90b:560d:b0:38e:9045:bac0 with SMTP id 98e67ed59e1d1-395deea6acbmr49754716a91.5.1787647744397; Tue, 25 Aug 2026 01:49:04 -0700 (PDT) Received: from G6L4RL2QG9.bytedance.net ([61.213.176.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39645da08desm2873386a91.17.2026.08.25.01.49.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 01:49:04 -0700 (PDT) From: Muchun Song To: Andrew Morton , Oscar Salvador , David Hildenbrand Cc: Mike Rapoport , Vlastimil Babka , Lorenzo Stoakes , Michal Hocko , David Laight , "Liam R . Howlett" , Suren Baghdasaryan , Qi Zheng , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Muchun Song Subject: [PATCH v5 01/17] mm/sparse: relax struct mem_section size constraints Date: Tue, 25 Aug 2026 16:45:52 +0800 Message-ID: <20260825084608.47437-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260825084608.47437-1-songmuchun@bytedance.com> References: <20260825084608.47437-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 struct mem_section is currently forced to a power-of-2 size so the section-to-root lookup can use a mask instead of a modulo. That requirement makes future extensions harder than necessary: adding a small field can require configuration-dependent padding or layout checks just to preserve the lookup scheme. Keep the lookup correct for any struct mem_section size by using a plain modulo instead. Do not leave the layout entirely unconstrained, though. Keep struct mem_section double-word aligned so modest size changes, such as adding another word-sized field on 64-bit systems, still keep a compact and efficient layout. If future fields grow the structure beyond that sweet spot, the lookup remains correct; only the exact layout efficiency changes. Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) --- v2: - Align struct mem_section to power-of-2 where possible, but support non-power-of-2 lookup (suggested by David Laight) - Collect Acked-by from Mike Rapoport --- include/linux/mmzone.h | 11 +++-------- mm/sparse.c | 2 -- scripts/gdb/linux/mm.py | 6 ++---- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 94f9c3ff5416..0a2428714108 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2027,13 +2027,9 @@ struct mem_section { * section. (see page_ext.h about this.) */ struct page_ext *page_ext; - unsigned long pad; #endif - /* - * WARNING: mem_section must be a power-of-2 in size for the - * calculation and use of SECTION_ROOT_MASK to make sense. - */ -}; +/* Sacrifice minor padding space for efficient lookup. */ +} __aligned(2 * sizeof(unsigned long)); #ifdef CONFIG_SPARSEMEM_EXTREME #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section)) @@ -2043,7 +2039,6 @@ struct mem_section { #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT) #define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT) -#define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1) #ifdef CONFIG_SPARSEMEM_EXTREME extern struct mem_section **mem_section; @@ -2067,7 +2062,7 @@ static inline struct mem_section *__nr_to_section(unsigned long nr) if (!mem_section || !mem_section[root]) return NULL; #endif - return &mem_section[root][nr & SECTION_ROOT_MASK]; + return &mem_section[root][nr % SECTIONS_PER_ROOT]; } /* diff --git a/mm/sparse.c b/mm/sparse.c index 7c15406e77f5..c84b4c7b8c70 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -322,8 +322,6 @@ void __init sparse_init(void) unsigned long pnum_end, pnum_begin, map_count = 1; int nid_begin; - /* see include/linux/mmzone.h 'struct mem_section' definition */ - BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section))); memblocks_present(); if (compound_info_has_mask()) { diff --git a/scripts/gdb/linux/mm.py b/scripts/gdb/linux/mm.py index dffadccbb01d..da4e8e9655a6 100644 --- a/scripts/gdb/linux/mm.py +++ b/scripts/gdb/linux/mm.py @@ -70,7 +70,6 @@ class x86_page_ops(): self.SECTIONS_PER_ROOT = 1 self.NR_SECTION_ROOTS = DIV_ROUND_UP(self.NR_MEM_SECTIONS, self.SECTIONS_PER_ROOT) - self.SECTION_ROOT_MASK = self.SECTIONS_PER_ROOT - 1 try: self.SECTION_HAS_MEM_MAP = 1 << int(gdb.parse_and_eval('SECTION_HAS_MEM_MAP_BIT')) @@ -100,7 +99,7 @@ class x86_page_ops(): def __nr_to_section(self, nr): root = self.SECTION_NR_TO_ROOT(nr) mem_section = gdb.parse_and_eval("mem_section") - return mem_section[root][nr & self.SECTION_ROOT_MASK] + return mem_section[root][nr % self.SECTIONS_PER_ROOT] def pfn_to_section_nr(self, pfn): return pfn >> self.PFN_SECTION_SHIFT @@ -249,7 +248,6 @@ class aarch64_page_ops(): self.SECTIONS_PER_ROOT = 1 self.NR_SECTION_ROOTS = DIV_ROUND_UP(self.NR_MEM_SECTIONS, self.SECTIONS_PER_ROOT) - self.SECTION_ROOT_MASK = self.SECTIONS_PER_ROOT - 1 self.SUBSECTION_SHIFT = 21 self.SEBSECTION_SIZE = 1 << self.SUBSECTION_SHIFT self.PFN_SUBSECTION_SHIFT = self.SUBSECTION_SHIFT - self.PAGE_SHIFT @@ -304,7 +302,7 @@ class aarch64_page_ops(): def __nr_to_section(self, nr): root = self.SECTION_NR_TO_ROOT(nr) mem_section = gdb.parse_and_eval("mem_section") - return mem_section[root][nr & self.SECTION_ROOT_MASK] + return mem_section[root][nr % self.SECTIONS_PER_ROOT] def pfn_to_section_nr(self, pfn): return pfn >> self.PFN_SECTION_SHIFT -- 2.54.0