From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 867163D45CB for ; Mon, 20 Jul 2026 09:31:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784539916; cv=none; b=IfjQCJ4XFkreXdPT+Dpejc92fESmy1dCjkNG7A0deETcC+WXcd6AMG9jGZggtORjU7j71EgDzLvU+NMpPOt5JPLY3Z5n5kkhjTtqhkYBLgZJNwlXVoE/mdKiT99bBtilUfLAa11l6mORYkQET49SeuK07RI6LnHts2XD878/EPc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784539916; c=relaxed/simple; bh=rLofQWHjRYkso5/XmB2nkMHlMv/dzmF9AEWfCv08OcQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QSR1FLfQiFnqIQhO069YNcboVaM7j3fBA1uDzdWvZzwpoemyK1egXhPBfOMhd0oj2F39EsK7wNUmv8EXHst6UTAIbukgd0/JSszdFukVKhVcBUFLEiMaSoxKAxICbwsN/FauGUbtii2/1eSBVIOW61JVFoZ27fdIUxLwl4N2WLg= 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=R/eNVNt0; arc=none smtp.client-ip=209.85.214.170 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="R/eNVNt0" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2ce7d2adef4so94146685ad.3 for ; Mon, 20 Jul 2026 02:31:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1784539914; x=1785144714; 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=xuZ6BXpDFSgmVKQGs+ltcwR2zNvZZ9A7kJPisvEZrvg=; b=R/eNVNt0M0UTYurSq5OD0oCjOD2KAvlmAovL4+ERTGlpa0Jj8LUbzERl6wFKhAJQef 44CoNGJhCGePPG/OMJ5S/eY6ZyFbPD8mmVxLwIIfh7xKmmlaCfapphcPtZTTKC9URA+D DpkIcS1duhjH/1K2BLv393Mi8hWYkfM1EqmNHEPXVsuskS/hVfGFcxStmkjDjlibp1U6 J9+wmyyL71YQiNBZh0QxfRuE2sjXRk6suMg+NbNwl53xyK9ZChPYLZYSdrZXfBspWXJi HUohzbraZxBh5tzN/gdSFI8dxm9HIkZusJsHmaUDOJjwOWTUI9HacYHjbvYa8+qGCA3n cjDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784539914; x=1785144714; 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=xuZ6BXpDFSgmVKQGs+ltcwR2zNvZZ9A7kJPisvEZrvg=; b=qDLzCFLYEEfhMuSZ2H76Wu3zQHZepJQ4RI9k3v8DugXPxD3xYMNNRtaOssfH7dD9YA OakdGmAFRhIy8Hv/yhLbWyfS8j1vNedVhZ0Upgmgck33IdAnYYtujSJ0F7r11Ncis8Rq nMWcZeY0LogpD4t+SYbwvNb9sKu1A66i4RmL/SCsrtOQgU/3tokCj9gmdwzagOVOGT7r qSHWeLBcS46ROdmrcbbzw+biaWyvsM7PICcgbn+mpgzWu8xUuFA/4PL7xRvSQahqY39o c8Erl4coEu85yU5BXuL/tz08VHOIr7d/kSa4ZdIIdxoa0p1NW0yX5lPH0Z4777uC8GP3 Ittw== X-Forwarded-Encrypted: i=1; AHgh+RpZoNel75+7zUqByOwV3lZUSjf+UygLSOqBqs/qJiEiE1kc4rKzoIbT3riT1wCBBycqhyJsxjvP66Lcx8U=@vger.kernel.org X-Gm-Message-State: AOJu0Ywm1IV0zbjyIGr1s7PKXrf4HDRZEc8rwKd6C96a7qf3/rqRt5Uu c6XaXXrhwrotoyPK0AG3O+ZhveiVqQrQQda/b2UA19Ty3BuW1zME6wfEQ76Xd7vfyZs= X-Gm-Gg: AfdE7ckzl5mO0CPFIA67eW+4hDf9T84cUoYQQ22vsgGzferw1FSkxyFbXoXhSCZJq2Z jEwDKAevAZdvwpYn9nEdC/7FY1H/PjT6jcFT4rYfzfwC3WeG5mEHfHa1HE4ivRLRSF8n+zTjYWc WJyjnbYjIUGNcMgBrJSFmDb5gLN3HkiurheFFkAwT05p5MyetzEJBJG8V/7tAX+I7dop0jCYmmp Wrwr7mGaQIzzDrw8I3JhK120TVPTtZOFFCs+sZYMA7C0sLff0VDrQNx405o3qWBMS4/UB+hNOAx LS16VdKigEemoyK1Yn2g83vVEBJIOM2V0YcU/h5T0IQPT1ZxdpJbG76tYaL7sr9I1DNogGp2f8D I+bhKJmAxcfFgQfDNY03srnYrIKFZvuCi4QAldGXgF7N05Uo2oIlk+DNDGCwUyjpkPlRa72LFZe upOzJdrZ1fNNLn84+lsQfmdxk= X-Received: by 2002:a17:903:4405:b0:2ca:ca48:c380 with SMTP id d9443c01a7336-2cf34a07a97mr145241885ad.47.1784539913858; Mon, 20 Jul 2026 02:31:53 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.101]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2cf347a7c9fsm53136455ad.84.2026.07.20.02.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2026 02:31:53 -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 v2 01/17] mm/sparse: relax struct mem_section size constraints Date: Mon, 20 Jul 2026 17:31:11 +0800 Message-ID: <20260720093127.540540-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260720093127.540540-1-songmuchun@bytedance.com> References: <20260720093127.540540-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 --- v2: - Align struct mem_section to power-of-2 where possible, but support non-power-of-2 lookup (suggested by David Laight) --- 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 0507193b3ae3..04f3409edacb 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2016,13 +2016,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)) @@ -2032,7 +2028,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; @@ -2056,7 +2051,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]; } extern size_t mem_section_usage_size(void); diff --git a/mm/sparse.c b/mm/sparse.c index 704a9dec2b9a..ca9875f568d3 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -332,8 +332,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