Skip to contentSkip to conversation

powerpc/64: Set up a kernel stack for secondaries before cpu_restore()

PATCH Superseded Jordan Niethe 2 participants 4 messages Sep 22, 2020 PowerPC

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 powerpc/64: Set up a kernel stack for secondaries before cpu_restore()

Currently in generic_secondary_smp_init(), cur_cpu_spec->cpu_restore() is called before a stack has been set up in r1. This was previously fine as the cpu_restore() functions were implemented in assembly and did not use a stack. However commit 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features") used __restore_cpu_cpufeatures() as the cpu_restore() function for a device-tree features based cputable entry. This is a C function and hence uses a stack in r1.

generic_secondary_smp_init() is entered on the secondary cpus via the primary cpu using the OPAL call opal_start_cpu(). In OPAL, each hardware thread has its own stack. The OPAL call is ran in the primary's hardware thread. During the call, a job is scheduled on a secondary cpu that will start executing at the address of generic_secondary_smp_init(). Hence the value that will be left in r1 when the secondary cpu enters the kernel is part of that secondary cpu's individual OPAL stack. This means that __restore_cpu_cpufeatures() will write to that OPAL stack. This is not horribly bad as each hardware thread has its own stack and the call that enters the kernel from OPAL never returns, but it is still wrong and should be corrected.

Create the temp kernel stack before calling cpu_restore().

As noted by mpe, for a kexec boot, the secondary CPUs are released from the spin loop at address 0x60 by smp_release_cpus() and then jump to generic_secondary_smp_init(). The call to smp_release_cpus() is in setup_arch(), and it comes before the call to emergency_stack_init(). emergency_stack_init() allocates an emergency stack in the PACA for each CPU. This address in the PACA is what is used to set up the temp kernel stack in generic_secondary_smp_init(). Move releasing the secondary CPUs to after the PACAs have been allocated an emergency stack, otherwise the PACA stack pointer will contain garbage and hence the temp kernel stack created from it will be broken.

Fixes 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features")

---
v2: Add more detail to the commit message
v3: Release secondary CPUs after the emergency stack is created

---
 arch/powerpc/kernel/head_64.S      | 8 ++++----
 arch/powerpc/kernel/setup-common.c | 6 ++++--
 2 files changed, 8 insertions(+), 6 deletions(-)
arch/powerpc/kernel/head_64.S +4 -4
index 0e05a9a47a4b..4b7f4c6c2600 100644
@@ -420,6 +420,10 @@ generic_secondary_common_init:
 	/* From now on, r24 is expected to be logical cpuid */
 	mr	r24,r5
 
+	/* Create a temp kernel stack for use before relocation is on.	*/
+	ld	r1,PACAEMERGSP(r13)
+	subi	r1,r1,STACK_FRAME_OVERHEAD
+
 	/* See if we need to call a cpu state restore handler */
 	LOAD_REG_ADDR(r23, cur_cpu_spec)
 	ld	r23,0(r23)
@@ -448,10 +452,6 @@ generic_secondary_common_init:
 	sync				/* order paca.run and cur_cpu_spec */
 	isync				/* In case code patching happened */
 
-	/* Create a temp kernel stack for use before relocation is on.	*/
-	ld	r1,PACAEMERGSP(r13)
-	subi	r1,r1,STACK_FRAME_OVERHEAD
-
 	b	__secondary_start
 #endif /* SMP */
 
arch/powerpc/kernel/setup-common.c +4 -2
index 808ec9fab605..fff714e36b37 100644
@@ -919,8 +919,6 @@ void __init setup_arch(char **cmdline_p)
 
 	/* On BookE, setup per-core TLB data structures. */
 	setup_tlb_core_data();
-
-	smp_release_cpus();
 #endif
 
 	/* Print various info about the machine that has been gathered so far. */
@@ -944,6 +942,10 @@ void __init setup_arch(char **cmdline_p)
 	exc_lvl_early_init();
 	emergency_stack_init();
 
+#ifdef CONFIG_SMP
+	smp_release_cpus();
+#endif
+
 	initmem_init();
 
 	early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 2/2 powerpc/64s: Convert some cpu_setup() and cpu_restore() functions to C

The only thing keeping the cpu_setup() and cpu_restore() functions used in the cputable entries for Power7, Power8, Power9 and Power10 in assembly was cpu_restore() being called before there was a stack in generic_secondary_smp_init(). Commit ("powerpc/64: Set up a kernel stack for secondaries before cpu_restore()") means that it is now possible to use C.

Rewrite the functions in C so they are a little bit easier to read. This is not changing their functionality.

---
 arch/powerpc/include/asm/cpu_setup_power.h |  12 +
 arch/powerpc/kernel/cpu_setup_power.S      | 252 -------------------
 arch/powerpc/kernel/cpu_setup_power.c      | 269 +++++++++++++++++++++
 arch/powerpc/kernel/cputable.c             |  12 +-
 4 files changed, 285 insertions(+), 260 deletions(-)
 create mode 100644 arch/powerpc/include/asm/cpu_setup_power.h
 delete mode 100644 arch/powerpc/kernel/cpu_setup_power.S
 create mode 100644 arch/powerpc/kernel/cpu_setup_power.c
arch/powerpc/include/asm/cpu_setup_power.h new file +12 -0
new file mode 100644
index 000000000000..24be9131f803
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2020 IBM Corporation
+ */
+void __setup_cpu_power7(unsigned long offset, struct cpu_spec *spec);
+void __restore_cpu_power7(void);
+void __setup_cpu_power8(unsigned long offset, struct cpu_spec *spec);
+void __restore_cpu_power8(void);
+void __setup_cpu_power9(unsigned long offset, struct cpu_spec *spec);
+void __restore_cpu_power9(void);
+void __setup_cpu_power10(unsigned long offset, struct cpu_spec *spec);
+void __restore_cpu_power10(void);
arch/powerpc/kernel/cpu_setup_power.S deleted +0 -252

252 lines not on this page · raw message

arch/powerpc/kernel/cpu_setup_power.c new file +269 -0

269 lines not on this page · raw message

arch/powerpc/kernel/cputable.c +4 -8
index 2aa89c6b2896..26a56c9d6650 100644
@@ -59,19 +59,15 @@ extern void __setup_cpu_7410(unsigned long offset, struct cpu_spec* spec);
 extern void __setup_cpu_745x(unsigned long offset, struct cpu_spec* spec);
 #endif /* CONFIG_PPC32 */
 #ifdef CONFIG_PPC64
+#include <asm/cpu_setup_power.h>
 extern void __setup_cpu_ppc970(unsigned long offset, struct cpu_spec* spec);
 extern void __setup_cpu_ppc970MP(unsigned long offset, struct cpu_spec* spec);
 extern void __setup_cpu_pa6t(unsigned long offset, struct cpu_spec* spec);
 extern void __restore_cpu_pa6t(void);
 extern void __restore_cpu_ppc970(void);
-extern void __setup_cpu_power7(unsigned long offset, struct cpu_spec* spec);
-extern void __restore_cpu_power7(void);
-extern void __setup_cpu_power8(unsigned long offset, struct cpu_spec* spec);
-extern void __restore_cpu_power8(void);
-extern void __setup_cpu_power9(unsigned long offset, struct cpu_spec* spec);
-extern void __restore_cpu_power9(void);
-extern void __setup_cpu_power10(unsigned long offset, struct cpu_spec* spec);
-extern void __restore_cpu_power10(void);
+extern long __machine_check_early_realmode_p7(struct pt_regs *regs);
+extern long __machine_check_early_realmode_p8(struct pt_regs *regs);
+extern long __machine_check_early_realmode_p9(struct pt_regs *regs);
 #endif /* CONFIG_PPC64 */
 #if defined(CONFIG_E500)
 extern void __setup_cpu_e5500(unsigned long offset, struct cpu_spec* spec);
Signature · 1 line
2.17.1
#2 lore raw
In reply to Jordan Niethe

Le 22/09/2020 à 07:53, Jordan Niethe a écrit :

Quoted 84 lines

Currently in generic_secondary_smp_init(), cur_cpu_spec->cpu_restore() is called before a stack has been set up in r1. This was previously fine as the cpu_restore() functions were implemented in assembly and did not use a stack. However commit 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features") used __restore_cpu_cpufeatures() as the cpu_restore() function for a device-tree features based cputable entry. This is a C function and hence uses a stack in r1.

generic_secondary_smp_init() is entered on the secondary cpus via the primary cpu using the OPAL call opal_start_cpu(). In OPAL, each hardware thread has its own stack. The OPAL call is ran in the primary's hardware thread. During the call, a job is scheduled on a secondary cpu that will start executing at the address of generic_secondary_smp_init(). Hence the value that will be left in r1 when the secondary cpu enters the kernel is part of that secondary cpu's individual OPAL stack. This means that __restore_cpu_cpufeatures() will write to that OPAL stack. This is not horribly bad as each hardware thread has its own stack and the call that enters the kernel from OPAL never returns, but it is still wrong and should be corrected.

Create the temp kernel stack before calling cpu_restore().

As noted by mpe, for a kexec boot, the secondary CPUs are released from the spin loop at address 0x60 by smp_release_cpus() and then jump to generic_secondary_smp_init(). The call to smp_release_cpus() is in setup_arch(), and it comes before the call to emergency_stack_init(). emergency_stack_init() allocates an emergency stack in the PACA for each CPU. This address in the PACA is what is used to set up the temp kernel stack in generic_secondary_smp_init(). Move releasing the secondary CPUs to after the PACAs have been allocated an emergency stack, otherwise the PACA stack pointer will contain garbage and hence the temp kernel stack created from it will be broken.

Fixes 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features")

---
v2: Add more detail to the commit message
v3: Release secondary CPUs after the emergency stack is created

---
  arch/powerpc/kernel/head_64.S      | 8 ++++----
  arch/powerpc/kernel/setup-common.c | 6 ++++--
  2 files changed, 8 insertions(+), 6 deletions(-)
arch/powerpc/kernel/head_64.S +4 -4
index 0e05a9a47a4b..4b7f4c6c2600 100644
@@ -420,6 +420,10 @@ generic_secondary_common_init:
  	/* From now on, r24 is expected to be logical cpuid */
  	mr	r24,r5
 
+	/* Create a temp kernel stack for use before relocation is on.	*/
+	ld	r1,PACAEMERGSP(r13)
+	subi	r1,r1,STACK_FRAME_OVERHEAD
+
  	/* See if we need to call a cpu state restore handler */
  	LOAD_REG_ADDR(r23, cur_cpu_spec)
  	ld	r23,0(r23)
@@ -448,10 +452,6 @@ generic_secondary_common_init:
  	sync				/* order paca.run and cur_cpu_spec */
  	isync				/* In case code patching happened */
 
-	/* Create a temp kernel stack for use before relocation is on.	*/
-	ld	r1,PACAEMERGSP(r13)
-	subi	r1,r1,STACK_FRAME_OVERHEAD
-
  	b	__secondary_start
  #endif /* SMP */
 
arch/powerpc/kernel/setup-common.c +0 -2
index 808ec9fab605..fff714e36b37 100644
@@ -919,8 +919,6 @@ void __init setup_arch(char **cmdline_p)
 
  	/* On BookE, setup per-core TLB data structures. */
  	setup_tlb_core_data();
-
-	smp_release_cpus();
  #endif
 
  	/* Print various info about the machine that has been gathered so far. */

arch/powerpc/kernel/setup-common.c void __init setup_arch(char **cmdline_p) · line 947

Quoted 6 lines
arch/powerpc/kernel/setup-common.c +3 -0
@@ -944,6 +942,10 @@ void __init setup_arch(char **cmdline_p)
  	exc_lvl_early_init();
  	emergency_stack_init();
 
+#ifdef CONFIG_SMP
+	smp_release_cpus();
+#endif

Are you sure you need that #ifdef ?

In asm/smp.h, we have:

#if defined(CONFIG_PPC64) && (defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)) extern void smp_release_cpus(void);
#else
static inline void smp_release_cpus(void) { };
#endif

arch/powerpc/kernel/setup-common.c void __init setup_arch(char **cmdline_p) · line 951

Quoted 5 lines
arch/powerpc/kernel/setup-common.c +1 -0
+
  	initmem_init();
 
  	early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);

Christophe

#3 lore raw
Quoted 91 lines · Christophe Leroy
Quoted 87 lines · Jordan Niethe

Currently in generic_secondary_smp_init(), cur_cpu_spec->cpu_restore() is called before a stack has been set up in r1. This was previously fine as the cpu_restore() functions were implemented in assembly and did not use a stack. However commit 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features") used __restore_cpu_cpufeatures() as the cpu_restore() function for a device-tree features based cputable entry. This is a C function and hence uses a stack in r1.

generic_secondary_smp_init() is entered on the secondary cpus via the primary cpu using the OPAL call opal_start_cpu(). In OPAL, each hardware thread has its own stack. The OPAL call is ran in the primary's hardware thread. During the call, a job is scheduled on a secondary cpu that will start executing at the address of generic_secondary_smp_init(). Hence the value that will be left in r1 when the secondary cpu enters the kernel is part of that secondary cpu's individual OPAL stack. This means that __restore_cpu_cpufeatures() will write to that OPAL stack. This is not horribly bad as each hardware thread has its own stack and the call that enters the kernel from OPAL never returns, but it is still wrong and should be corrected.

Create the temp kernel stack before calling cpu_restore().

As noted by mpe, for a kexec boot, the secondary CPUs are released from the spin loop at address 0x60 by smp_release_cpus() and then jump to generic_secondary_smp_init(). The call to smp_release_cpus() is in setup_arch(), and it comes before the call to emergency_stack_init(). emergency_stack_init() allocates an emergency stack in the PACA for each CPU. This address in the PACA is what is used to set up the temp kernel stack in generic_secondary_smp_init(). Move releasing the secondary CPUs to after the PACAs have been allocated an emergency stack, otherwise the PACA stack pointer will contain garbage and hence the temp kernel stack created from it will be broken.

Fixes 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features")

---
v2: Add more detail to the commit message
v3: Release secondary CPUs after the emergency stack is created

---
  arch/powerpc/kernel/head_64.S      | 8 ++++----
  arch/powerpc/kernel/setup-common.c | 6 ++++--
  2 files changed, 8 insertions(+), 6 deletions(-)
arch/powerpc/kernel/head_64.S +4 -4
index 0e05a9a47a4b..4b7f4c6c2600 100644
@@ -420,6 +420,10 @@ generic_secondary_common_init:
      /* From now on, r24 is expected to be logical cpuid */
      mr      r24,r5
 
+     /* Create a temp kernel stack for use before relocation is on.  */
+     ld      r1,PACAEMERGSP(r13)
+     subi    r1,r1,STACK_FRAME_OVERHEAD
+
      /* See if we need to call a cpu state restore handler */
      LOAD_REG_ADDR(r23, cur_cpu_spec)
      ld      r23,0(r23)
@@ -448,10 +452,6 @@ generic_secondary_common_init:
      sync                            /* order paca.run and cur_cpu_spec */
      isync                           /* In case code patching happened */
 
-     /* Create a temp kernel stack for use before relocation is on.  */
-     ld      r1,PACAEMERGSP(r13)
-     subi    r1,r1,STACK_FRAME_OVERHEAD
-
      b       __secondary_start
  #endif /* SMP */
 
arch/powerpc/kernel/setup-common.c +0 -2
index 808ec9fab605..fff714e36b37 100644
@@ -919,8 +919,6 @@ void __init setup_arch(char **cmdline_p)
 
      /* On BookE, setup per-core TLB data structures. */
      setup_tlb_core_data();
-
-     smp_release_cpus();
  #endif
 
      /* Print various info about the machine that has been gathered so far. */
@@ -944,6 +942,10 @@ void __init setup_arch(char **cmdline_p)
      exc_lvl_early_init();
      emergency_stack_init();
 
Quoted 5 lines

arch/powerpc/kernel/setup-common.c void __init setup_arch(char **cmdline_p) · line 947

Quoted 3 lines
arch/powerpc/kernel/setup-common.c +3 -0
+#ifdef CONFIG_SMP
+     smp_release_cpus();
+#endif

Are you sure you need that #ifdef ?

Thanks, you are right, should not be necessary.

Quoted 18 lines

In asm/smp.h, we have:

#if defined(CONFIG_PPC64) && (defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE))
extern void smp_release_cpus(void);
#else
static inline void smp_release_cpus(void) { }; #endif

arch/powerpc/kernel/setup-common.c void __init setup_arch(char **cmdline_p) · line 951

Quoted 5 lines
arch/powerpc/kernel/setup-common.c +1 -0
+
      initmem_init();
 
      early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);

Christophe