[PATCH] mm/mm_init.c: remove spinlock in early_pfn_to_nid()

From: Yajun Deng
Date: Wed Jun 14 2023 - 07:04:02 EST


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@xxxxxxxxx>
---
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