Skip to contentSkip to conversation

drivers/base/memory: Skip handled sections in memory_dev_init()

PATCH New Gavin Shan 1 participant 1 message May 8, 2025

Subsystems (3)
lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH 1/1 drivers/base/memory: Skip handled sections in memory_dev_init()

It's unnecessary to iterate those sections and check their presence states if their block has been handled. In this specific case, the cursor of for_each_present_section_nr() can leap to the starting section of next block to bypass those covered sections, resulting in small performance gain. For example, the time consumed by the function drops by 12us on a X86 system with 100GB memory as Oscar tested.

---
 drivers/base/memory.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/base/memory.c +11 -11
index 19469e7f88c2..f612e43afe39 100644
@@ -942,7 +942,7 @@ static const struct attribute_group *memory_root_attr_groups[] = {
 void __init memory_dev_init(void)
 {
 	int ret;
-	unsigned long block_sz, block_id, nr;
+	unsigned long block_sz, nr;
 
 	/* Validate the configured memory block size */
 	block_sz = memory_block_size_bytes();
@@ -956,22 +956,22 @@ void __init memory_dev_init(void)
 
 	/*
 	 * Create entries for memory sections that were found during boot
-	 * and have been initialized. Use @block_id to track the last
-	 * handled block and initialize it to an invalid value (ULONG_MAX)
-	 * to bypass the block ID matching check for the first present
-	 * block so that it can be covered.
+	 * and have been initialized.
 	 */
-	block_id = ULONG_MAX;
 	for_each_present_section_nr(0, nr) {
-		if (block_id != ULONG_MAX && memory_block_id(nr) == block_id)
-			continue;
-
-		block_id = memory_block_id(nr);
-		ret = add_memory_block(block_id, MEM_ONLINE, NULL, NULL);
+		ret = add_memory_block(memory_block_id(nr), MEM_ONLINE,
+				       NULL, NULL);
 		if (ret) {
 			panic("%s() failed to add memory block: %d\n",
 			      __func__, ret);
 		}
+
+		/*
+		 * Forward to the last section in this block so that we will
+		 * process the first section of the next block in the next
+		 * iteration.
+		 */
+		nr = ALIGN(nr + 1, sections_per_block) - 1;
 	}
 }
 
Signature · 1 line
2.49.0