Skip to content

Commit c78f94f

Browse files
atishp04palmer-dabbelt
authored andcommitted
RISC-V: Use __cpu_up_stack/task_pointer only for spinwait method
The __cpu_up_stack/task_pointer array is only used for spinwait method now. The per cpu array based lookup is also fragile for platforms with discontiguous/sparse hartids. The spinwait method is only used for M-mode Linux or older firmwares without SBI HSM extension. For general Linux systems, ordered booting method is preferred anyways to support cpu hotplug and kexec. Make sure that __cpu_up_stack/task_pointer is only used for spinwait method. Take this opportunity to rename it to __cpu_spinwait_stack/task_pointer to emphasize the purpose as well. Reviewed-by: Anup Patel <anup@brainfault.org> Signed-off-by: Atish Patra <atishp@rivosinc.com> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 410bb20 commit c78f94f

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

arch/riscv/include/asm/cpu_ops.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,5 @@ struct cpu_operations {
4040

4141
extern const struct cpu_operations *cpu_ops[NR_CPUS];
4242
void __init cpu_set_ops(int cpu);
43-
void cpu_update_secondary_bootdata(unsigned int cpuid,
44-
struct task_struct *tidle);
4543

4644
#endif /* ifndef __ASM_CPU_OPS_H */

arch/riscv/kernel/cpu_ops.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,15 @@
88
#include <linux/of.h>
99
#include <linux/string.h>
1010
#include <linux/sched.h>
11-
#include <linux/sched/task_stack.h>
1211
#include <asm/cpu_ops.h>
1312
#include <asm/sbi.h>
1413
#include <asm/smp.h>
1514

1615
const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
1716

18-
void *__cpu_up_stack_pointer[NR_CPUS] __section(".data");
19-
void *__cpu_up_task_pointer[NR_CPUS] __section(".data");
20-
2117
extern const struct cpu_operations cpu_ops_sbi;
2218
extern const struct cpu_operations cpu_ops_spinwait;
2319

24-
void cpu_update_secondary_bootdata(unsigned int cpuid,
25-
struct task_struct *tidle)
26-
{
27-
int hartid = cpuid_to_hartid_map(cpuid);
28-
29-
/* Make sure tidle is updated */
30-
smp_mb();
31-
WRITE_ONCE(__cpu_up_stack_pointer[hartid],
32-
task_stack_page(tidle) + THREAD_SIZE);
33-
WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
34-
}
35-
3620
void __init cpu_set_ops(int cpuid)
3721
{
3822
#if IS_ENABLED(CONFIG_RISCV_SBI)

arch/riscv/kernel/cpu_ops_spinwait.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,36 @@
66
#include <linux/errno.h>
77
#include <linux/of.h>
88
#include <linux/string.h>
9+
#include <linux/sched/task_stack.h>
910
#include <asm/cpu_ops.h>
1011
#include <asm/sbi.h>
1112
#include <asm/smp.h>
1213

1314
const struct cpu_operations cpu_ops_spinwait;
15+
void *__cpu_spinwait_stack_pointer[NR_CPUS] __section(".data");
16+
void *__cpu_spinwait_task_pointer[NR_CPUS] __section(".data");
17+
18+
static void cpu_update_secondary_bootdata(unsigned int cpuid,
19+
struct task_struct *tidle)
20+
{
21+
int hartid = cpuid_to_hartid_map(cpuid);
22+
23+
/*
24+
* The hartid must be less than NR_CPUS to avoid out-of-bound access
25+
* errors for __cpu_spinwait_stack/task_pointer. That is not always possible
26+
* for platforms with discontiguous hartid numbering scheme. That's why
27+
* spinwait booting is not the recommended approach for any platforms
28+
* booting Linux in S-mode and can be disabled in the future.
29+
*/
30+
if (hartid == INVALID_HARTID || hartid >= NR_CPUS)
31+
return;
32+
33+
/* Make sure tidle is updated */
34+
smp_mb();
35+
WRITE_ONCE(__cpu_spinwait_stack_pointer[hartid],
36+
task_stack_page(tidle) + THREAD_SIZE);
37+
WRITE_ONCE(__cpu_spinwait_task_pointer[hartid], tidle);
38+
}
1439

1540
static int spinwait_cpu_prepare(unsigned int cpuid)
1641
{
@@ -28,7 +53,7 @@ static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle)
2853
* selects the first cpu to boot the kernel and causes the remainder
2954
* of the cpus to spin in a loop waiting for their stack pointer to be
3055
* setup by that main cpu. Writing to bootdata
31-
* (i.e __cpu_up_stack_pointer) signals to the spinning cpus that they
56+
* (i.e __cpu_spinwait_stack_pointer) signals to the spinning cpus that they
3257
* can continue the boot process.
3358
*/
3459
cpu_update_secondary_bootdata(cpuid, tidle);

arch/riscv/kernel/head.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ clear_bss_done:
347347
csrw CSR_TVEC, a3
348348

349349
slli a3, a0, LGREG
350-
la a1, __cpu_up_stack_pointer
350+
la a1, __cpu_spinwait_stack_pointer
351351
XIP_FIXUP_OFFSET a1
352-
la a2, __cpu_up_task_pointer
352+
la a2, __cpu_spinwait_task_pointer
353353
XIP_FIXUP_OFFSET a2
354354
add a1, a3, a1
355355
add a2, a3, a2

arch/riscv/kernel/head.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa);
1616
asmlinkage void __init __copy_data(void);
1717
#endif
1818

19-
extern void *__cpu_up_stack_pointer[];
20-
extern void *__cpu_up_task_pointer[];
19+
extern void *__cpu_spinwait_stack_pointer[];
20+
extern void *__cpu_spinwait_task_pointer[];
2121

2222
#endif /* __ASM_HEAD_H */

0 commit comments

Comments
 (0)