From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E723C54E65 for ; Thu, 22 May 2025 10:37:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9716F6B0082; Thu, 22 May 2025 06:37:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FAFD6B0083; Thu, 22 May 2025 06:37:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C3396B0085; Thu, 22 May 2025 06:37:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5D5E16B0082 for ; Thu, 22 May 2025 06:37:11 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C788A1A0119 for ; Thu, 22 May 2025 10:37:10 +0000 (UTC) X-FDA: 83470191420.13.02A34B8 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf26.hostedemail.com (Postfix) with ESMTP id 2B3D7140003 for ; Thu, 22 May 2025 10:37:09 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Aac2C0Lt; spf=pass (imf26.hostedemail.com: domain of rppt@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747910229; h=from:from:sender: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:dkim-signature; bh=i+TKuM3yuF1nJ7WBJfpYm0w8KgCMyhtyH/1FN9nxmJE=; b=sxPAOmoU7J+FDSNUi9u2oKlcwkB18EEYB9TK8cXVYhAPBn3NxTRCcSVuMajruw0NkphPBz Tn+90GFMBAbanZyz8Of+B/WbhEdQ+zMbq9CVbEEaE4JDPG2XfPBur9g6BJAsbfl0Xyl03k WjBkLBb5lCf5mDd/4+OwgFze3kJEpt8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Aac2C0Lt; spf=pass (imf26.hostedemail.com: domain of rppt@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747910229; a=rsa-sha256; cv=none; b=EyjLP+UV7aPLLoEseAB1tJSiBfA8H5stwumzGDvjKWMGcrKiym542LQoQ4fRgu3PVsHb86 yi9skuFS7P6uuubWYUOampHjGE8x7o1KXczy60pkw5Y1whX8zGFNNA4RiM98/sAeB/MUM2 /h7jm9fRhsOAvURkf0z/FJzSbwoStDo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 3CB9762A38; Thu, 22 May 2025 10:37:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EBB7DC4CEE4; Thu, 22 May 2025 10:37:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747910227; bh=R9eVqf3ocvCkF7oOCf8M5BDeTpo98BblFckyQ7qS2kE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Aac2C0Lt3lj5HVNfQYo6I80TuK7F4iSkB0eCFTAyoNlhkitkBv9prcjDBXue3ONYj 0C0WT6Nxxu/+2FudCFt66w3Tc1jwzlXjgm97/yKG7XMhrcGKCuXRbYJ1sWWYEkBCi7 0TFFZHZ+64RLI9DZSG4FQY8NfLOXTH0gAZnND/GnHEdHb7JIbd7mTm+y3iRmerMf2t B6zNMzh0bQi8uuSIGwDgsfQlgxQB/8ryyvAQHM7/wOOt28SLJBpBJydRjtnNLAWOJt ObVABYHvdXQBE/poi40J0T7cn8zaCUzLhiswWfKkg0xZtW6JT/Ye7JSgUYF6vnlSUA 6KWN2cxvFqSdg== Date: Thu, 22 May 2025 13:36:59 +0300 From: Mike Rapoport To: Donet Tom Cc: David Hildenbrand , Andrew Morton , Oscar Salvador , Zi Yan , Greg Kroah-Hartman , Ritesh Harjani , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Rafael J . Wysocki" , Danilo Krummrich , Jonathan Cameron , Alison Schofield , Yury Norov , Dave Jiang Subject: Re: [PATCH v5 1/4] drivers/base/node: Optimize memory block registration to reduce boot time Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 2B3D7140003 X-Stat-Signature: nrd9babwcdj746o181zbmqjwk7ok7ano X-Rspam-User: X-HE-Tag: 1747910229-52507 X-HE-Meta: U2FsdGVkX1+UvFxlIL/buSa74Fh6G6g5Vc4AtQ6u0O178WUmmIRC7H2uEFY7syIzSjDi4fXRSH4biZqy1KC5i4B9tbfFermwNs3GbrPB7yT7I3pjvUIjJxvU6lDumQz86q6dTU2fh2QLFvlKwAkZonb4Z488PoP10TohfeZ3hLCWO1j3viJwHstWhDgDnibQ6HoPNhtyK01lD/+9xI7FhV7AlVvm/JJQEIEfAxV7SHHAuSdnsnGOyfzAepUwtMumN7MdFe4ReuLqXSZZk9KN+kBvqBHtHDzeeEE65rIyUrSeNvsUXm1UlWNSBPLcoPPQgxvMQM84Kcwyz4fv7tZMat3aqDvA+9WH7Lj8BoGdQFZjhzgoKUT1qI/6ZLsUvnA0MncNANudmsaElOJzQutw2+Ctk3FjL2bLPNzCICFdY5c2z+0MVI56nKHTMjhQv4cnLc5BBvFLPKvABiz7ODvOIw5G0us0C7DlawC+JgaSsIyLj/zt7IZn433ims7XFR1WbdOSUXa6pvI0NwZ3T2nsH5tWc8rTZDMcIDi2fIyQb4Go0YsobGGPZEzvyXUxdBm0r8V9TvzwsXB7wXy6vOpz/cEb45N/kOMkgu5Q6T+FVOZS4zy6XgjYD9GWm25JmyVQq+TiAadzTHKI7tpDVMcI+n5a8EL4/WfjOlfTbUPgSFLhkkuANzkiduO6KTGsI6OXFNc9ONClNLRUy3BFXpMTPUTl3keuU7GZHL+MptHrZe97dkWLd9s2F72nBmUP7irKxonB3qFDyOqxyti7Q4lg4NNTSX4KNQWseOEUNjpIEiFPG5vzhcVdnMCVGN31jX+m4Gb6zia7egWozEm9d9qGP7YJJik44EPNbKKiUNgOzn7IwJOaz7fJTopoI570OQYfmX+Wwb8gFeG0Vs+4ENLXwLLVK+DPwI833875N7QlV5dq3oRuX7hda/3hph6hqrprurouKzchWh+Zr8/FzHD ZWTdK1FS lv5TUNe7YteLvt6iBKjiYow7AS8Z09xstvCOOBZMTYCZBqwIAbcuyzBKMMsxqet4Ip97o89fYQL1lWHpqdOuUI3sroTBWI2st1UuATQmiswFleQQS+Rlsnm8Rp12Wb+k1YpiPdChWkdMa1vaqoSj8N/HaV34VJtinKJ+XNAXDc1HYBBEwIEfJONdURF8AKYYJTG6K3WEmmbGifgy8re7O1xWmpFuUu6QvzZrCnZ16i9611LTsFu2GgZ86HbRpStmJHsE1oi9MyjGRo64MkJfvly4zF0wliqLVBpcSM96JTvlPkknPmJEWmGKRDUM/YWPRXwBx901BS5nL532Kxhb0A6r1PAeUDJkgOTbqvDD5zhkm1zMMDqlIiNXXrBCUIsxwGAPaVa+ZVQJaKhb+rFxphuFcd26v1Gm+EVogK1l794tH7h4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, May 22, 2025 at 04:17:28AM -0500, Donet Tom wrote: > During node device initialization, `memory blocks` are registered under > each NUMA node. The `memory blocks` to be registered are identified using > the node’s start and end PFNs, which are obtained from the node's pg_data > > However, not all PFNs within this range necessarily belong to the same > node—some may belong to other nodes. Additionally, due to the > discontiguous nature of physical memory, certain sections within a > `memory block` may be absent. > > As a result, `memory blocks` that fall between a node’s start and end > PFNs may span across multiple nodes, and some sections within those blocks > may be missing. `Memory blocks` have a fixed size, which is architecture > dependent. > > Due to these considerations, the memory block registration is currently > performed as follows: > > for_each_online_node(nid): > start_pfn = pgdat->node_start_pfn; > end_pfn = pgdat->node_start_pfn + node_spanned_pages; > for_each_memory_block_between(PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)) > mem_blk = memory_block_id(pfn_to_section_nr(pfn)); > pfn_mb_start=section_nr_to_pfn(mem_blk->start_section_nr) > pfn_mb_end = pfn_start + memory_block_pfns - 1 > for (pfn = pfn_mb_start; pfn < pfn_mb_end; pfn++): > if (get_nid_for_pfn(pfn) != nid): > continue; > else > do_register_memory_block_under_node(nid, mem_blk, > MEMINIT_EARLY); > > Here, we derive the start and end PFNs from the node's pg_data, then > determine the memory blocks that may belong to the node. For each > `memory block` in this range, we inspect all PFNs it contains and check > their associated NUMA node ID. If a PFN within the block matches the > current node, the memory block is registered under that node. > > If CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, get_nid_for_pfn() performs > a binary search in the `memblock regions` to determine the NUMA node ID > for a given PFN. If it is not enabled, the node ID is retrieved directly > from the struct page. > > On large systems, this process can become time-consuming, especially since > we iterate over each `memory block` and all PFNs within it until a match is > found. When CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, the additional > overhead of the binary search increases the execution time significantly, > potentially leading to soft lockups during boot. > > In this patch, we iterate over `memblock region` to identify the > `memory blocks` that belong to the current NUMA node. `memblock regions` > are contiguous memory ranges, each associated with a single NUMA node, and > they do not span across multiple nodes. > > for_each_memory_region(r): // r => region > if (!node_online(r->nid)): > continue; > else > for_each_memory_block_between(r->base, r->base + r->size - 1): > do_register_memory_block_under_node(r->nid, mem_blk, MEMINIT_EARLY); > > We iterate over all memblock regions, and if the node associated with the > region is online, we calculate the start and end memory blocks based on the > region's start and end PFNs. We then register all the memory blocks within > that range under the region node. > > Test Results on My system with 32TB RAM > ======================================= > 1. Boot time with CONFIG_DEFERRED_STRUCT_PAGE_INIT enabled. > > Without this patch > ------------------ > Startup finished in 1min 16.528s (kernel) > > With this patch > --------------- > Startup finished in 17.236s (kernel) - 78% Improvement > > 2. Boot time with CONFIG_DEFERRED_STRUCT_PAGE_INIT disabled. > > Without this patch > ------------------ > Startup finished in 28.320s (kernel) > > With this patch > --------------- > Startup finished in 15.621s (kernel) - 46% Improvement > > Acked-by: Zi Yan > Signed-off-by: Donet Tom > > --- > v4 -> v5 > > 1. Moved all helpers(memory_block_id(), pfn_to_block_id(), and phys_to_block_id()) > into memory.h and exported sections_per_block. > 2. register_memory_blocks_early() moved out of for_each_online_node(). > Now we iterate over all memory regions at once and register the > memory blocks. > > Tested corner cases where memory blocks span across multiple memblock regions; it > is working fine. > > #cd /sys/devices/system/node/ > # find node1/ |grep memory0 > node1/memory0 > # find node0/ |grep memory0 > node0/memory0 > # find node0/ |grep memory0 > node2/memory0 > # cat node0/memory0/valid_zones > none > > V4 - https://lore.kernel.org/all/f94685be9cdc931a026999d236d7e92de29725c7.1747376551.git.donettom@linux.ibm.com/ > V3 - https://lore.kernel.org/all/b49ed289096643ff5b5fbedcf1d1c1be42845a74.1746250339.git.donettom@linux.ibm.com/ > v2 - https://lore.kernel.org/all/fbe1e0c7d91bf3fa9a64ff5d84b53ded1d0d5ac7.1745852397.git.donettom@linux.ibm.com/ > v1 - https://lore.kernel.org/all/50142a29010463f436dc5c4feb540e5de3bb09df.1744175097.git.donettom@linux.ibm.com/ > --- > drivers/base/memory.c | 21 ++++---------------- > drivers/base/node.c | 45 ++++++++++++++++++++++++++++++++++++++++-- > include/linux/memory.h | 19 +++++++++++++++++- > include/linux/node.h | 3 +++ > 4 files changed, 68 insertions(+), 20 deletions(-) > > diff --git a/drivers/base/memory.c b/drivers/base/memory.c > index 19469e7f88c2..39fcc075a36f 100644 > --- a/drivers/base/memory.c > +++ b/drivers/base/memory.c > @@ -22,6 +22,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -48,22 +49,8 @@ int mhp_online_type_from_str(const char *str) > > #define to_memory_block(dev) container_of(dev, struct memory_block, dev) > > -static int sections_per_block; > - > -static inline unsigned long memory_block_id(unsigned long section_nr) > -{ > - return section_nr / sections_per_block; > -} > - > -static inline unsigned long pfn_to_block_id(unsigned long pfn) > -{ > - return memory_block_id(pfn_to_section_nr(pfn)); > -} > - > -static inline unsigned long phys_to_block_id(unsigned long phys) > -{ > - return pfn_to_block_id(PFN_DOWN(phys)); > -} > +int sections_per_block; > +EXPORT_SYMBOL(sections_per_block); > > static int memory_subsys_online(struct device *dev); > static int memory_subsys_offline(struct device *dev); > @@ -632,7 +619,7 @@ int __weak arch_get_memory_phys_device(unsigned long start_pfn) > * > * Called under device_hotplug_lock. > */ > -static struct memory_block *find_memory_block_by_id(unsigned long block_id) > +struct memory_block *find_memory_block_by_id(unsigned long block_id) > { > struct memory_block *mem; > > diff --git a/drivers/base/node.c b/drivers/base/node.c > index cd13ef287011..e8b6f6b9ce51 100644 > --- a/drivers/base/node.c > +++ b/drivers/base/node.c > @@ -20,6 +20,7 @@ > #include > #include > #include > +#include > > static const struct bus_type node_subsys = { > .name = "node", > @@ -850,6 +851,41 @@ void unregister_memory_block_under_nodes(struct memory_block *mem_blk) > kobject_name(&node_devices[mem_blk->nid]->dev.kobj)); > } > > +/* > + * register_memory_blocks_under_node_early : Register the memory blocks > + * under the nodes. > + * > + * This function iterates over all memblock regions, and if the node associated with > + * the region is online, calculates the start and end memory blocks based on the > + * region's start and end PFNs. Then, registers all the memory blocks within that > + * range under the region node. > + */ > +static void register_memory_blocks_under_node_early(void) > +{ > + struct memblock_region *r; > + > + for_each_mem_region(r) { > + const unsigned long start_block_id = phys_to_block_id(r->base); > + const unsigned long end_block_id = phys_to_block_id(r->base + r->size - 1); > + unsigned long block_id; > + > + if (!node_online(r->nid)) memblock_get_region_node() please, otherwise it won't build for !NUMA. Otherwise Acked-by: Mike Rapoport (Microsoft) > + continue; > + > + for (block_id = start_block_id; block_id <= end_block_id; block_id++) { > + struct memory_block *mem; > + > + mem = find_memory_block_by_id(block_id); > + if (!mem) > + continue; > + > + do_register_memory_block_under_node(r->nid, mem, MEMINIT_EARLY); > + put_device(&mem->dev); > + } > + > + } > +} > + > void register_memory_blocks_under_node(int nid, unsigned long start_pfn, > unsigned long end_pfn, > enum meminit_context context) > @@ -971,11 +1007,16 @@ void __init node_dev_init(void) > > /* > * Create all node devices, which will properly link the node > - * to applicable memory block devices and already created cpu devices. > + * to already created cpu devices. > */ > for_each_online_node(i) { > - ret = register_one_node(i); > + ret = __register_one_node(i); > if (ret) > panic("%s() failed to add node: %d\n", __func__, ret); > } > + > + /* > + * Link the node to memory block devices > + */ > + register_memory_blocks_under_node_early(); > } > diff --git a/include/linux/memory.h b/include/linux/memory.h > index 12daa6ec7d09..2a61088e17ad 100644 > --- a/include/linux/memory.h > +++ b/include/linux/memory.h > @@ -171,12 +171,30 @@ struct memory_group *memory_group_find_by_id(int mgid); > typedef int (*walk_memory_groups_func_t)(struct memory_group *, void *); > int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, > struct memory_group *excluded, void *arg); > +struct memory_block *find_memory_block_by_id(unsigned long block_id); > #define hotplug_memory_notifier(fn, pri) ({ \ > static __meminitdata struct notifier_block fn##_mem_nb =\ > { .notifier_call = fn, .priority = pri };\ > register_memory_notifier(&fn##_mem_nb); \ > }) > > +extern int sections_per_block; > + > +static inline unsigned long memory_block_id(unsigned long section_nr) > +{ > + return section_nr / sections_per_block; > +} > + > +static inline unsigned long pfn_to_block_id(unsigned long pfn) > +{ > + return memory_block_id(pfn_to_section_nr(pfn)); > +} > + > +static inline unsigned long phys_to_block_id(unsigned long phys) > +{ > + return pfn_to_block_id(PFN_DOWN(phys)); > +} > + > #ifdef CONFIG_NUMA > void memory_block_add_nid(struct memory_block *mem, int nid, > enum meminit_context context); > @@ -188,5 +206,4 @@ void memory_block_add_nid(struct memory_block *mem, int nid, > * can sleep. > */ > extern struct mutex text_mutex; > - > #endif /* _LINUX_MEMORY_H_ */ > diff --git a/include/linux/node.h b/include/linux/node.h > index 2b7517892230..5c763253c42c 100644 > --- a/include/linux/node.h > +++ b/include/linux/node.h > @@ -120,6 +120,9 @@ static inline void register_memory_blocks_under_node(int nid, unsigned long star > enum meminit_context context) > { > } > +static inline void register_memory_blocks_under_node_early(void) > +{ > +} > #endif > > extern void unregister_node(struct node *node); > -- > 2.43.5 > -- Sincerely yours, Mike.