Skip to content

Commit 3d8c1a0

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64: smp: initialize cpu offset earlier
Now that we have a consistent place to initialize CPU context registers early in the boot path, let's also initialize the per-cpu offset here. This makes the primary and secondary boot paths more consistent, and allows for the use of per-cpu operations earlier, which will be necessary for instrumentation with KCSAN. Note that smp_prepare_boot_cpu() still needs to re-initialize CPU0's offset as immediately prior to this the per-cpu areas may be reallocated, and hence the boot-time offset may be stale. A comment is added to make this clear. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: James Morse <james.morse@arm.com> Cc: Marc Zyngier <maz@kernel.org> Cc: Suzuki Poulose <suzuki.poulose@arm.com> Cc: Will Deacon <will@kernel.org> Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20210520115031.18509-7-mark.rutland@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 8e334d7 commit 3d8c1a0

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

arch/arm64/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
int main(void)
2828
{
2929
DEFINE(TSK_ACTIVE_MM, offsetof(struct task_struct, active_mm));
30+
DEFINE(TSK_CPU, offsetof(struct task_struct, cpu));
3031
BLANK();
3132
DEFINE(TSK_TI_FLAGS, offsetof(struct task_struct, thread_info.flags));
3233
DEFINE(TSK_TI_PREEMPT, offsetof(struct task_struct, thread_info.preempt_count));

arch/arm64/kernel/head.S

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,22 @@ SYM_FUNC_END(__create_page_tables)
402402
* its location in the task stack. We reserve the entire pt_regs space
403403
* for consistency with user tasks and kthreads.
404404
*/
405-
.macro init_cpu_task tsk, tmp
405+
.macro init_cpu_task tsk, tmp1, tmp2
406406
msr sp_el0, \tsk
407407

408-
ldr \tmp, [\tsk, #TSK_STACK]
409-
add sp, \tmp, #THREAD_SIZE
408+
ldr \tmp1, [\tsk, #TSK_STACK]
409+
add sp, \tmp1, #THREAD_SIZE
410410
sub sp, sp, #PT_REGS_SIZE
411411

412412
stp xzr, xzr, [sp, #S_STACKFRAME]
413413
add x29, sp, #S_STACKFRAME
414414

415-
scs_load \tsk, \tmp
415+
scs_load \tsk, \tmp1
416+
417+
adr_l \tmp1, __per_cpu_offset
418+
ldr w\tmp2, [\tsk, #TSK_CPU]
419+
ldr \tmp1, [\tmp1, \tmp2, lsl #3]
420+
set_this_cpu_offset \tmp1
416421
.endm
417422

418423
/*
@@ -422,7 +427,7 @@ SYM_FUNC_END(__create_page_tables)
422427
*/
423428
SYM_FUNC_START_LOCAL(__primary_switched)
424429
adr_l x4, init_task
425-
init_cpu_task x4, x5
430+
init_cpu_task x4, x5, x6
426431

427432
adr_l x8, vectors // load VBAR_EL1 with virtual
428433
msr vbar_el1, x8 // vector table address
@@ -650,7 +655,7 @@ SYM_FUNC_START_LOCAL(__secondary_switched)
650655
ldr x2, [x0, #CPU_BOOT_TASK]
651656
cbz x2, __secondary_too_slow
652657

653-
init_cpu_task x2, x1
658+
init_cpu_task x2, x1, x3
654659

655660
#ifdef CONFIG_ARM64_PTR_AUTH
656661
ptrauth_keys_init_cpu x2, x3, x4, x5

arch/arm64/kernel/setup.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,6 @@ void __init smp_setup_processor_id(void)
8787
u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
8888
set_cpu_logical_map(0, mpidr);
8989

90-
/*
91-
* clear __my_cpu_offset on boot CPU to avoid hang caused by
92-
* using percpu variable early, for example, lockdep will
93-
* access percpu variable inside lock_release
94-
*/
95-
set_my_cpu_offset(0);
9690
pr_info("Booting Linux on physical CPU 0x%010lx [0x%08x]\n",
9791
(unsigned long)mpidr, read_cpuid_id());
9892
}

arch/arm64/kernel/smp.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,7 @@ asmlinkage notrace void secondary_start_kernel(void)
198198
u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
199199
struct mm_struct *mm = &init_mm;
200200
const struct cpu_operations *ops;
201-
unsigned int cpu;
202-
203-
cpu = task_cpu(current);
204-
set_my_cpu_offset(per_cpu_offset(cpu));
201+
unsigned int cpu = smp_processor_id();
205202

206203
/*
207204
* All kernel threads share the same mm context; grab a
@@ -448,6 +445,11 @@ void __init smp_cpus_done(unsigned int max_cpus)
448445

449446
void __init smp_prepare_boot_cpu(void)
450447
{
448+
/*
449+
* The runtime per-cpu areas have been allocated by
450+
* setup_per_cpu_areas(), and CPU0's boot time per-cpu area will be
451+
* freed shortly, so we must move over to the runtime per-cpu area.
452+
*/
451453
set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
452454
cpuinfo_store_boot_cpu();
453455

0 commit comments

Comments
 (0)