Skip to content

Commit 383fb3e

Browse files
author
Russell King
committed
ARM: spectre-v2: per-CPU vtables to work around big.Little systems
In big.Little systems, some CPUs require the Spectre workarounds in paths such as the context switch, but other CPUs do not. In order to handle these differences, we need per-CPU vtables. We are unable to use the kernel's per-CPU variables to support this as per-CPU is not initialised at times when we need access to the vtables, so we have to use an array indexed by logical CPU number. We use an array-of-pointers to avoid having function pointers in the kernel's read/write .data section. Reviewed-by: Julien Thierry <julien.thierry@arm.com> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
1 parent e209950 commit 383fb3e

4 files changed

Lines changed: 61 additions & 15 deletions

File tree

‎arch/arm/include/asm/proc-fns.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,35 @@ extern void cpu_do_resume(void *);
104104
#else
105105

106106
extern struct processor processor;
107+
#if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR)
108+
#include <linux/smp.h>
109+
/*
110+
* This can't be a per-cpu variable because we need to access it before
111+
* per-cpu has been initialised. We have a couple of functions that are
112+
* called in a pre-emptible context, and so can't use smp_processor_id()
113+
* there, hence PROC_TABLE(). We insist in init_proc_vtable() that the
114+
* function pointers for these are identical across all CPUs.
115+
*/
116+
extern struct processor *cpu_vtable[];
117+
#define PROC_VTABLE(f) cpu_vtable[smp_processor_id()]->f
118+
#define PROC_TABLE(f) cpu_vtable[0]->f
119+
static inline void init_proc_vtable(const struct processor *p)
120+
{
121+
unsigned int cpu = smp_processor_id();
122+
*cpu_vtable[cpu] = *p;
123+
WARN_ON_ONCE(cpu_vtable[cpu]->dcache_clean_area !=
124+
cpu_vtable[0]->dcache_clean_area);
125+
WARN_ON_ONCE(cpu_vtable[cpu]->set_pte_ext !=
126+
cpu_vtable[0]->set_pte_ext);
127+
}
128+
#else
107129
#define PROC_VTABLE(f) processor.f
108130
#define PROC_TABLE(f) processor.f
109131
static inline void init_proc_vtable(const struct processor *p)
110132
{
111133
processor = *p;
112134
}
135+
#endif
113136

114137
#define cpu_proc_init PROC_VTABLE(_proc_init)
115138
#define cpu_check_bugs PROC_VTABLE(check_bugs)

‎arch/arm/kernel/setup.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ EXPORT_SYMBOL(elf_hwcap2);
115115

116116
#ifdef MULTI_CPU
117117
struct processor processor __ro_after_init;
118+
#if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR)
119+
struct processor *cpu_vtable[NR_CPUS] = {
120+
[0] = &processor,
121+
};
122+
#endif
118123
#endif
119124
#ifdef MULTI_TLB
120125
struct cpu_tlb_fns cpu_tlb __ro_after_init;

‎arch/arm/kernel/smp.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <asm/mmu_context.h>
4343
#include <asm/pgtable.h>
4444
#include <asm/pgalloc.h>
45+
#include <asm/procinfo.h>
4546
#include <asm/processor.h>
4647
#include <asm/sections.h>
4748
#include <asm/tlbflush.h>
@@ -102,13 +103,41 @@ static unsigned long get_arch_pgd(pgd_t *pgd)
102103
#endif
103104
}
104105

106+
#if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR)
107+
static int secondary_biglittle_prepare(unsigned int cpu)
108+
{
109+
if (!cpu_vtable[cpu])
110+
cpu_vtable[cpu] = kzalloc(sizeof(*cpu_vtable[cpu]), GFP_KERNEL);
111+
112+
return cpu_vtable[cpu] ? 0 : -ENOMEM;
113+
}
114+
115+
static void secondary_biglittle_init(void)
116+
{
117+
init_proc_vtable(lookup_processor(read_cpuid_id())->proc);
118+
}
119+
#else
120+
static int secondary_biglittle_prepare(unsigned int cpu)
121+
{
122+
return 0;
123+
}
124+
125+
static void secondary_biglittle_init(void)
126+
{
127+
}
128+
#endif
129+
105130
int __cpu_up(unsigned int cpu, struct task_struct *idle)
106131
{
107132
int ret;
108133

109134
if (!smp_ops.smp_boot_secondary)
110135
return -ENOSYS;
111136

137+
ret = secondary_biglittle_prepare(cpu);
138+
if (ret)
139+
return ret;
140+
112141
/*
113142
* We need to tell the secondary core where to find
114143
* its stack and the page tables.
@@ -360,6 +389,8 @@ asmlinkage void secondary_start_kernel(void)
360389
struct mm_struct *mm = &init_mm;
361390
unsigned int cpu;
362391

392+
secondary_biglittle_init();
393+
363394
/*
364395
* The identity mapping is uncached (strongly ordered), so
365396
* switch away from it before attempting any exclusive accesses.

‎arch/arm/mm/proc-v7-bugs.c‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,13 @@ static void cpu_v7_spectre_init(void)
5252
case ARM_CPU_PART_CORTEX_A17:
5353
case ARM_CPU_PART_CORTEX_A73:
5454
case ARM_CPU_PART_CORTEX_A75:
55-
if (processor.switch_mm != cpu_v7_bpiall_switch_mm)
56-
goto bl_error;
5755
per_cpu(harden_branch_predictor_fn, cpu) =
5856
harden_branch_predictor_bpiall;
5957
spectre_v2_method = "BPIALL";
6058
break;
6159

6260
case ARM_CPU_PART_CORTEX_A15:
6361
case ARM_CPU_PART_BRAHMA_B15:
64-
if (processor.switch_mm != cpu_v7_iciallu_switch_mm)
65-
goto bl_error;
6662
per_cpu(harden_branch_predictor_fn, cpu) =
6763
harden_branch_predictor_iciallu;
6864
spectre_v2_method = "ICIALLU";
@@ -88,11 +84,9 @@ static void cpu_v7_spectre_init(void)
8884
ARM_SMCCC_ARCH_WORKAROUND_1, &res);
8985
if ((int)res.a0 != 0)
9086
break;
91-
if (processor.switch_mm != cpu_v7_hvc_switch_mm && cpu)
92-
goto bl_error;
9387
per_cpu(harden_branch_predictor_fn, cpu) =
9488
call_hvc_arch_workaround_1;
95-
processor.switch_mm = cpu_v7_hvc_switch_mm;
89+
cpu_do_switch_mm = cpu_v7_hvc_switch_mm;
9690
spectre_v2_method = "hypervisor";
9791
break;
9892

@@ -101,11 +95,9 @@ static void cpu_v7_spectre_init(void)
10195
ARM_SMCCC_ARCH_WORKAROUND_1, &res);
10296
if ((int)res.a0 != 0)
10397
break;
104-
if (processor.switch_mm != cpu_v7_smc_switch_mm && cpu)
105-
goto bl_error;
10698
per_cpu(harden_branch_predictor_fn, cpu) =
10799
call_smc_arch_workaround_1;
108-
processor.switch_mm = cpu_v7_smc_switch_mm;
100+
cpu_do_switch_mm = cpu_v7_smc_switch_mm;
109101
spectre_v2_method = "firmware";
110102
break;
111103

@@ -119,11 +111,6 @@ static void cpu_v7_spectre_init(void)
119111
if (spectre_v2_method)
120112
pr_info("CPU%u: Spectre v2: using %s workaround\n",
121113
smp_processor_id(), spectre_v2_method);
122-
return;
123-
124-
bl_error:
125-
pr_err("CPU%u: Spectre v2: incorrect context switching function, system vulnerable\n",
126-
cpu);
127114
}
128115
#else
129116
static void cpu_v7_spectre_init(void)

0 commit comments

Comments
 (0)