Skip to content

Commit 353e228

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64: initialize per-cpu offsets earlier
The current initialization of the per-cpu offset register is difficult to follow and this initialization is not always early enough for upcoming instrumentation with KCSAN, where the instrumentation callbacks use the per-cpu offset. To make it possible to support KCSAN, and to simplify reasoning about early bringup code, let's initialize the per-cpu offset earlier, before we run any C code that may consume it. To do so, this patch adds a new init_this_cpu_offset() helper that's called before the usual primary/secondary start functions. For consistency, this is also used to re-initialize the per-cpu offset after the runtime per-cpu areas have been allocated (which can change CPU0's offset). So that init_this_cpu_offset() isn't subject to any instrumentation that might consume the per-cpu offset, it is marked with noinstr, preventing instrumentation. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: James Morse <james.morse@arm.com> Cc: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20201005164303.21389-1-mark.rutland@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 4dafc08 commit 353e228

4 files changed

Lines changed: 19 additions & 11 deletions

File tree

arch/arm64/include/asm/cpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ void __init init_cpu_features(struct cpuinfo_arm64 *info);
6868
void update_cpu_features(int cpu, struct cpuinfo_arm64 *info,
6969
struct cpuinfo_arm64 *boot);
7070

71+
void init_this_cpu_offset(void);
72+
7173
#endif /* __ASM_CPU_H */

arch/arm64/kernel/head.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ SYM_FUNC_START_LOCAL(__primary_switched)
452452
bl __pi_memset
453453
dsb ishst // Make zero page visible to PTW
454454

455+
bl init_this_cpu_offset
456+
455457
#ifdef CONFIG_KASAN
456458
bl kasan_early_init
457459
#endif
@@ -758,6 +760,7 @@ SYM_FUNC_START_LOCAL(__secondary_switched)
758760
ptrauth_keys_init_cpu x2, x3, x4, x5
759761
#endif
760762

763+
bl init_this_cpu_offset
761764
b secondary_start_kernel
762765
SYM_FUNC_END(__secondary_switched)
763766

arch/arm64/kernel/setup.c

Lines changed: 6 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
}
@@ -282,6 +276,12 @@ u64 cpu_logical_map(int cpu)
282276
}
283277
EXPORT_SYMBOL_GPL(cpu_logical_map);
284278

279+
void noinstr init_this_cpu_offset(void)
280+
{
281+
unsigned int cpu = task_cpu(current);
282+
set_my_cpu_offset(per_cpu_offset(cpu));
283+
}
284+
285285
void __init __no_sanitize_address setup_arch(char **cmdline_p)
286286
{
287287
init_mm.start_code = (unsigned long) _text;

arch/arm64/kernel/smp.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,7 @@ asmlinkage notrace void secondary_start_kernel(void)
192192
u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
193193
struct mm_struct *mm = &init_mm;
194194
const struct cpu_operations *ops;
195-
unsigned int cpu;
196-
197-
cpu = task_cpu(current);
198-
set_my_cpu_offset(per_cpu_offset(cpu));
195+
unsigned int cpu = smp_processor_id();
199196

200197
/*
201198
* All kernel threads share the same mm context; grab a
@@ -435,7 +432,13 @@ void __init smp_cpus_done(unsigned int max_cpus)
435432

436433
void __init smp_prepare_boot_cpu(void)
437434
{
438-
set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
435+
/*
436+
* Now that setup_per_cpu_areas() has allocated the runtime per-cpu
437+
* areas it is only safe to read the CPU0 boot-time area, and we must
438+
* reinitialize the offset to point to the runtime area.
439+
*/
440+
init_this_cpu_offset();
441+
439442
cpuinfo_store_boot_cpu();
440443

441444
/*

0 commit comments

Comments
 (0)