[PATCH] drivers/base/node.c: optimize get_nid_for_pfn()
From: David Hildenbrand
Date: Wed Nov 27 2019 - 10:18:42 EST
Since commit d84f2f5a7552 ("drivers/base/node.c: simplify
unregister_memory_block_under_nodes()") we only have a single user of
get_nid_for_pfn(). The remaining user calls this function when booting -
where all added memory is online.
Make it clearer that this function should only be used during boot (
e.g., calling it on offline memory would be bad) by renaming the
function to something meaningful, optimize out the ifdef and the additional
system_state check, and add a comment why CONFIG_DEFERRED_STRUCT_PAGE_INIT
handling is in place at all.
Also, optimize the call site. There is no need to check against
page_nid < 0 - it will never match the nid (nid >=3D 0).
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Cc: "Rafael J. Wysocki" <rafael@xxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxxxx>
Cc: Oscar Salvador <osalvador@xxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: David Hildenbrand <david@xxxxxxxxxx>
---
drivers/base/node.c | 22 +++++++++-------------
1 file changed, 9 insertions(+), 13 deletions(-)
diff --git a/drivers/base/node.c b/drivers/base/node.c
index 98a31bafc8a2..d525e30581de 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -744,14 +744,16 @@ int unregister_cpu_under_node(unsigned int cpu, unsig=
ned int nid)
}
=20
#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
-static int __ref get_nid_for_pfn(unsigned long pfn)
+static int __ref boot_pfn_to_nid(unsigned long pfn)
{
=09if (!pfn_valid_within(pfn))
=09=09return -1;
-#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
-=09if (system_state < SYSTEM_RUNNING)
+=09/*
+=09 * With deferred struct page initialization, the memmap will contain
+=09 * garbage - we have to rely on the early nid.
+=09 */
+=09if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT))
=09=09return early_pfn_to_nid(pfn);
-#endif
=09return pfn_to_nid(pfn);
}
=20
@@ -766,8 +768,6 @@ static int register_mem_sect_under_node(struct memory_b=
lock *mem_blk,
=09unsigned long pfn;
=20
=09for (pfn =3D start_pfn; pfn <=3D end_pfn; pfn++) {
-=09=09int page_nid;
-
=09=09/*
=09=09 * memory block could have several absent sections from start.
=09=09 * skip pfn range from absent section
@@ -783,13 +783,9 @@ static int register_mem_sect_under_node(struct memory_=
block *mem_blk,
=09=09 * case, during hotplug we know that all pages in the memory
=09=09 * block belong to the same node.
=09=09 */
-=09=09if (system_state =3D=3D SYSTEM_BOOTING) {
-=09=09=09page_nid =3D get_nid_for_pfn(pfn);
-=09=09=09if (page_nid < 0)
-=09=09=09=09continue;
-=09=09=09if (page_nid !=3D nid)
-=09=09=09=09continue;
-=09=09}
+=09=09if (system_state =3D=3D SYSTEM_BOOTING &&
+=09=09 boot_pfn_to_nid(pfn) !=3D nid)
+=09=09=09continue;
=20
=09=09/*
=09=09 * If this memory block spans multiple nodes, we only indicate
--=20
2.21.0
--=20
Thanks,
David / dhildenb