PowerPC currently associates a CPU device with its device-tree node from
register_cpu_online(), which runs in the CPUHP_AP_ONLINE_DYN range.

Generic cacheinfo runs earlier at CPUHP_AP_BASE_CACHEINFO_ONLINE. For a
CPU added through pSeries DLPAR, the CPU device already exists but its
of_node is still NULL. Consequently, of_cpu_device_node_get() cannot find
the CPU node and generic cacheinfo prevents the CPU from coming online.

Add an explicit PowerPC CPU hotplug state immediately before
CPUHP_AP_BASE_CACHEINFO_ONLINE and move the CPU-device OF-node association
there. Keep the remaining PowerPC sysfs setup at its existing dynamic
state.

This also provides the correct teardown ordering: generic cacheinfo is
removed before PowerPC releases the CPU device's OF-node reference.

[Both draft changelog and code are written by LLM under guidance]
Assisted-by: LLM
Signed-off-by: Shrikanth Hegde <[email protected]>
---
 arch/powerpc/kernel/sysfs.c | 51 ++++++++++++++++++++++++++++++++-----
 include/linux/cpuhotplug.h  |  1 +
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/arch/powerpc/kernel/sysfs.c b/arch/powerpc/kernel/sysfs.c
index 2100c5e2c821..092312163b35 100644
--- a/arch/powerpc/kernel/sysfs.c
+++ b/arch/powerpc/kernel/sysfs.c
@@ -834,6 +834,40 @@ static void remove_idle_spurr_file(struct device *s)
 #define remove_idle_spurr_file(s)
 #endif /* CONFIG_PPC_PSERIES */
 
+static int register_cpu_of_node(unsigned int cpu)
+{
+       struct device *dev = get_cpu_device(cpu);
+
+       if (!dev)
+               return -ENODEV;
+
+       /*
+        * CPUs present at boot already acquire this reference from
+        * register_cpu(). DLPAR-added and re-onlined CPUs may not have
+        * the OF node associated with the CPU device yet.
+        */
+       if (!dev->of_node)
+               dev->of_node = of_get_cpu_node(cpu, NULL);
+
+       if (!dev->of_node)
+               return -ENOENT;
+
+       return 0;
+}
+
+static int unregister_cpu_of_node(unsigned int cpu)
+{
+       struct device *dev = get_cpu_device(cpu);
+
+       if (!dev)
+               return 0;
+
+       of_node_put(dev->of_node);
+       dev->of_node = NULL;
+
+       return 0;
+}
+
 static int register_cpu_online(unsigned int cpu)
 {
        struct cpu *c = &per_cpu(cpu_devices, cpu);
@@ -841,10 +875,6 @@ static int register_cpu_online(unsigned int cpu)
        struct device_attribute *attrs, *pmc_attrs;
        int i, nattrs;
 
-       /* For cpus present at boot a reference was already grabbed in 
register_cpu() */
-       if (!s->of_node)
-               s->of_node = of_get_cpu_node(cpu, NULL);
-
 #ifdef CONFIG_PPC64
        if (cpu_has_feature(CPU_FTR_SMT))
                device_create_file(s, &dev_attr_smt_snooze_delay);
@@ -1025,8 +1055,6 @@ static int unregister_cpu_online(unsigned int cpu)
        }
 #endif
        cacheinfo_cpu_offline(cpu);
-       of_node_put(s->of_node);
-       s->of_node = NULL;
        return 0;
 }
 #else /* !CONFIG_HOTPLUG_CPU */
@@ -1173,6 +1201,17 @@ static int __init topology_init(void)
                        device_create_file(&c->dev, &dev_attr_physical_id);
                }
        }
+
+       /*
+        * This must precede generic cacheinfo so
+        * of_cpu_device_node_get() can use cpu_dev->of_node.
+        */
+       r = cpuhp_setup_state_nocalls(CPUHP_AP_POWERPC_OF_NODE_ONLINE,
+                              "powerpc/of_node:online",
+                              register_cpu_of_node,
+                              unregister_cpu_of_node);
+       WARN_ON(r < 0);
+
        r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/topology:online",
                              register_cpu_online, unregister_cpu_online);
        WARN_ON(r < 0);
diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
index 1b6dba159bb2..617ad3cde57f 100644
--- a/include/linux/cpuhotplug.h
+++ b/include/linux/cpuhotplug.h
@@ -231,6 +231,7 @@ enum cpuhp_state {
        CPUHP_AP_RANDOM_ONLINE,
        CPUHP_AP_RCUTREE_ONLINE,
        CPUHP_AP_KTHREADS_ONLINE,
+       CPUHP_AP_POWERPC_OF_NODE_ONLINE,
        CPUHP_AP_BASE_CACHEINFO_ONLINE,
        CPUHP_AP_ONLINE_DYN,
        CPUHP_AP_ONLINE_DYN_END         = CPUHP_AP_ONLINE_DYN + 40,
-- 
2.52.0


Reply via email to