linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Gavin Shan <gshan@redhat.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, david@redhat.com,
	osalvador@suse.de, gregkh@linuxfoundation.org, rafael@kernel.org,
	dakr@kernel.org
Subject: [PATCH] drivers/base/memory: Skip handled sections in memory_dev_init()
Date: Thu,  8 May 2025 10:08:01 +1000	[thread overview]
Message-ID: <20250508000801.178672-1-gshan@redhat.com> (raw)

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.

Suggested-by: Oscar Salvador <osalvador@suse.de>
Signed-off-by: Gavin Shan <gshan@redhat.com>
---
 drivers/base/memory.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 19469e7f88c2..f612e43afe39 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -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;
 	}
 }
 
-- 
2.49.0



                 reply	other threads:[~2025-05-08  0:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250508000801.178672-1-gshan@redhat.com \
    --to=gshan@redhat.com \
    --cc=dakr@kernel.org \
    --cc=david@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=osalvador@suse.de \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).