linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yajun Deng <yajun.deng@linux.dev>
To: gregkh@linuxfoundation.org, rafael@kernel.org, rppt@kernel.org,
	akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Yajun Deng <yajun.deng@linux.dev>
Subject: [PATCH] mm/mm_init.c: remove spinlock in early_pfn_to_nid()
Date: Wed, 14 Jun 2023 19:03:24 +0800	[thread overview]
Message-ID: <20230614110324.3839354-1-yajun.deng@linux.dev> (raw)

When the system boots, only one cpu is enabled before smp_init().
So the spinlock is not needed in most cases, remove it.

Add spinlock in get_nid_for_pfn() because it is after smp_init().

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
 drivers/base/node.c | 11 +++++++++--
 mm/mm_init.c        | 18 +++---------------
 2 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/drivers/base/node.c b/drivers/base/node.c
index 9de524e56307..844102570ff2 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -748,8 +748,15 @@ int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
 static int __ref get_nid_for_pfn(unsigned long pfn)
 {
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
-	if (system_state < SYSTEM_RUNNING)
-		return early_pfn_to_nid(pfn);
+	static DEFINE_SPINLOCK(early_pfn_lock);
+	int nid;
+
+	if (system_state < SYSTEM_RUNNING) {
+		spin_lock(&early_pfn_lock);
+		nid = early_pfn_to_nid(pfn);
+		spin_unlock(&early_pfn_lock);
+		return nid;
+	}
 #endif
 	return pfn_to_nid(pfn);
 }
diff --git a/mm/mm_init.c b/mm/mm_init.c
index d393631599a7..5895a30435ee 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -584,11 +584,11 @@ struct mminit_pfnnid_cache {
 static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
 
 /*
- * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
+ * Given a PFN, return what node the PFN is on.
  */
-static int __meminit __early_pfn_to_nid(unsigned long pfn,
-					struct mminit_pfnnid_cache *state)
+int __meminit early_pfn_to_nid(unsigned long pfn)
 {
+	struct mminit_pfnnid_cache *state = &early_pfnnid_cache;
 	unsigned long start_pfn, end_pfn;
 	int nid;
 
@@ -601,20 +601,8 @@ static int __meminit __early_pfn_to_nid(unsigned long pfn,
 		state->last_end = end_pfn;
 		state->last_nid = nid;
 	}
-
-	return nid;
-}
-
-int __meminit early_pfn_to_nid(unsigned long pfn)
-{
-	static DEFINE_SPINLOCK(early_pfn_lock);
-	int nid;
-
-	spin_lock(&early_pfn_lock);
-	nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
 	if (nid < 0)
 		nid = first_online_node;
-	spin_unlock(&early_pfn_lock);
 
 	return nid;
 }
-- 
2.25.1


             reply	other threads:[~2023-06-14 11:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-14 11:03 Yajun Deng [this message]
2023-06-14 11:09 ` [PATCH] mm/mm_init.c: remove spinlock in early_pfn_to_nid() Greg KH
2023-06-14 11:28 ` Yajun Deng
2023-06-14 11:53   ` Mike Rapoport
2023-06-15  3:02   ` Yajun Deng
2023-06-15  6:20     ` Mike Rapoport
2023-06-15  6:36     ` Yajun Deng

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=20230614110324.3839354-1-yajun.deng@linux.dev \
    --to=yajun.deng@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rafael@kernel.org \
    --cc=rppt@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).