From patchwork Thu Feb 14 02:10:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Long X-Patchwork-Id: 158288 Delivered-To: patches@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp793915jaa; Wed, 13 Feb 2019 18:10:45 -0800 (PST) X-Received: by 2002:a0c:8204:: with SMTP id h4mr1028567qva.85.1550110245710; Wed, 13 Feb 2019 18:10:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550110245; cv=none; d=google.com; s=arc-20160816; b=WYko5/77sduQLBrFXJAG/iMg8FIkyULi511BdwSGJ/nRFyBvCw+xq7AyKO1uW/5qZj Ia8bH/P/v9hJch/cu5bRIYmXeS91QPpqnbXhjl2zb6Cw3TjDIkbYcYhrN7UI40faIBQp fNm9mDLK1CR2EVDhwVYvXfXEkh+9AwNIzO1pcLLnAOhzgSkbddUKr/49MalwaF5aDdun 4gDuV8+JYIEjJTscyWp3FZF1LAAGJlPV77U4TzmpEJbKRyd0elSqgOUcXuRk39RTuJKX OQDfUvyKpSpLibt/gRSGllvQjuNu2GLCKJADkyN5hv0hs7pDWzPy2uB+u/7rzhmHi+5w 498w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2Lgm0wW67bNBbj1r22lApgse7tqbhFKA3zBeaNlbhCI=; b=TtIJuHoU9keYgaYUCoNWidff/yCM3rQkxgOWIXz6kJBnEJ7YNTnmjv2yCZ7KQyr9n/ ESOihAheF0z+38QdOhSMm75iqlpDhqBSGcP8+SWItDXPtjdPwVIu/EKQYo83nMIkMji5 q38O8LJFl3qin5TDKDAAznSyIrr5rfjJd6itYsBKsezwhpiGFpwzDID3W1ESQFU1bIFw ru777fqBdnHYP11D0tBnHd5X8AEXkJY4DphiIkDkkGIlVza7OAhFKOVQJEzJDIevs2Co CcMN4E2IUL7baJyn2a97rzi1336uRFYTCtHbCwEqxJHZLHY5HWpjE0cVcg2ir6ooujRI keAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZqtC6ztQ; spf=pass (google.com: domain of dave.long@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=dave.long@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id j137sor634476qke.70.2019.02.13.18.10.45 for (Google Transport Security); Wed, 13 Feb 2019 18:10:45 -0800 (PST) Received-SPF: pass (google.com: domain of dave.long@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZqtC6ztQ; spf=pass (google.com: domain of dave.long@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=dave.long@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2Lgm0wW67bNBbj1r22lApgse7tqbhFKA3zBeaNlbhCI=; b=ZqtC6ztQKzKjRT17/NHjp51BgwHp2P8zTR/KjjBZOi/aBmnf9GCVYZNFqDu+ZQbYyE AVOZlG91yPwbr581JY3vXpnEr2bs+63WcoJCsBmXD5jhAIttu6X6WCRZR0hlXOQ6PbKW xy+CNGXQkVV48z2q6THpiVdP8NmIgdHD+UdCmfvvhf24CGp/9FTXxCVJjaN/rnYqQw1b 1ZzNba3O20YsITg9X+IEBK41YViocpVXqjqustQdMSP1VMjea8lxgB/Uqmb3u+JL5Kjo LpXtM/FKU9lofgbNq/gMQrxW5Ny81IR8WZDf2MtgiBS0EGYYM+qKCu2etzp0Lt1+ux1o MPzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2Lgm0wW67bNBbj1r22lApgse7tqbhFKA3zBeaNlbhCI=; b=eOCpNFtsgLxtEbu2MK7hrhrc6MKJkc2Xx7GlsqsjR7BccQxbqasF/FP22rQYXZ4Xh/ Ngbhhm9e3FUsBIQ1aT2ildfRyR3zRK+XuKwL8u9hQpXKF14ejlWensZJOXezC8binj0d Wn92o6ItqDpfUYto/8qOWZdgBZv28HNs62WWk377Re/UzT1DSgcvXmjniB3jPKtAVefG BdfNC+bMx2qJo7j4hlWbfNTQ5B2KwdLPXO1cyolaGC7Z9cL8r0IRR98f9hqeHQGSsybz gM5Py0ZNONkRzofIkYlHXPZqhPXLSvp0FTIu0dKP6uBb9mapmvyWv5eRNOxLPOddGCru iEkQ== X-Gm-Message-State: AHQUAuYaYdDgPc5+aDvciEQb4m+BHRU8Nftl/rKl9JUEousip/A+2lSq D4W0FRvSZ9syg2mgRkD5zMBwR1sY2ALrPA== X-Google-Smtp-Source: AHgI3IbkIMKirzSeRlp6sjp1u6jkDWXic77jGrmcuLXEQ6FKZZRn66QwCFwx2jC1GA/ASOvB8ZhGbQ== X-Received: by 2002:a37:4145:: with SMTP id o66mr921103qka.129.1550110245375; Wed, 13 Feb 2019 18:10:45 -0800 (PST) Return-Path: Received: from localhost.localdomain (pool-72-71-243-63.cncdnh.fast00.myfairpoint.net. [72.71.243.63]) by smtp.googlemail.com with ESMTPSA id d63sm1091575qka.22.2019.02.13.18.10.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 18:10:44 -0800 (PST) From: David Long To: stable@vger.kernel.org, Russell King - ARM Linux , Florian Fainelli , Julien Thierry , Tony Lindgren , Marc Zyngier , Greg KH , Mark Rutland Cc: Will Deacon , Mark Brown , linux-kernel@vger.kernel.org Subject: [PATCH 4.14 V2 15/17] ARM: spectre-v2: per-CPU vtables to work around big.Little systems Date: Wed, 13 Feb 2019 21:10:20 -0500 Message-Id: <20190214021022.5313-16-dave.long@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190214021022.5313-1-dave.long@linaro.org> References: <20190214021022.5313-1-dave.long@linaro.org> From: Russell King Commit 383fb3ee8024d596f488d2dbaf45e572897acbdb upstream. 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 Signed-off-by: Russell King Signed-off-by: David A. Long --- arch/arm/include/asm/proc-fns.h | 23 +++++++++++++++++++++++ arch/arm/kernel/setup.c | 5 +++++ arch/arm/kernel/smp.c | 31 +++++++++++++++++++++++++++++++ arch/arm/mm/proc-v7-bugs.c | 17 ++--------------- 4 files changed, 61 insertions(+), 15 deletions(-) -- 2.17.1 diff --git a/arch/arm/include/asm/proc-fns.h b/arch/arm/include/asm/proc-fns.h index c259cc49c641..e1b6f280ab08 100644 --- a/arch/arm/include/asm/proc-fns.h +++ b/arch/arm/include/asm/proc-fns.h @@ -104,12 +104,35 @@ extern void cpu_do_resume(void *); #else extern struct processor processor; +#if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR) +#include +/* + * This can't be a per-cpu variable because we need to access it before + * per-cpu has been initialised. We have a couple of functions that are + * called in a pre-emptible context, and so can't use smp_processor_id() + * there, hence PROC_TABLE(). We insist in init_proc_vtable() that the + * function pointers for these are identical across all CPUs. + */ +extern struct processor *cpu_vtable[]; +#define PROC_VTABLE(f) cpu_vtable[smp_processor_id()]->f +#define PROC_TABLE(f) cpu_vtable[0]->f +static inline void init_proc_vtable(const struct processor *p) +{ + unsigned int cpu = smp_processor_id(); + *cpu_vtable[cpu] = *p; + WARN_ON_ONCE(cpu_vtable[cpu]->dcache_clean_area != + cpu_vtable[0]->dcache_clean_area); + WARN_ON_ONCE(cpu_vtable[cpu]->set_pte_ext != + cpu_vtable[0]->set_pte_ext); +} +#else #define PROC_VTABLE(f) processor.f #define PROC_TABLE(f) processor.f static inline void init_proc_vtable(const struct processor *p) { processor = *p; } +#endif #define cpu_proc_init PROC_VTABLE(_proc_init) #define cpu_check_bugs PROC_VTABLE(check_bugs) diff --git a/arch/arm/kernel/setup.c b/arch/arm/kernel/setup.c index c1588e31aa83..a6d27284105a 100644 --- a/arch/arm/kernel/setup.c +++ b/arch/arm/kernel/setup.c @@ -115,6 +115,11 @@ EXPORT_SYMBOL(elf_hwcap2); #ifdef MULTI_CPU struct processor processor __ro_after_init; +#if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR) +struct processor *cpu_vtable[NR_CPUS] = { + [0] = &processor, +}; +#endif #endif #ifdef MULTI_TLB struct cpu_tlb_fns cpu_tlb __ro_after_init; diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c index 5e31c62127a0..f57333f46242 100644 --- a/arch/arm/kernel/smp.c +++ b/arch/arm/kernel/smp.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -102,6 +103,30 @@ static unsigned long get_arch_pgd(pgd_t *pgd) #endif } +#if defined(CONFIG_BIG_LITTLE) && defined(CONFIG_HARDEN_BRANCH_PREDICTOR) +static int secondary_biglittle_prepare(unsigned int cpu) +{ + if (!cpu_vtable[cpu]) + cpu_vtable[cpu] = kzalloc(sizeof(*cpu_vtable[cpu]), GFP_KERNEL); + + return cpu_vtable[cpu] ? 0 : -ENOMEM; +} + +static void secondary_biglittle_init(void) +{ + init_proc_vtable(lookup_processor(read_cpuid_id())->proc); +} +#else +static int secondary_biglittle_prepare(unsigned int cpu) +{ + return 0; +} + +static void secondary_biglittle_init(void) +{ +} +#endif + int __cpu_up(unsigned int cpu, struct task_struct *idle) { int ret; @@ -109,6 +134,10 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) if (!smp_ops.smp_boot_secondary) return -ENOSYS; + ret = secondary_biglittle_prepare(cpu); + if (ret) + return ret; + /* * We need to tell the secondary core where to find * its stack and the page tables. @@ -360,6 +389,8 @@ asmlinkage void secondary_start_kernel(void) struct mm_struct *mm = &init_mm; unsigned int cpu; + secondary_biglittle_init(); + /* * The identity mapping is uncached (strongly ordered), so * switch away from it before attempting any exclusive accesses. diff --git a/arch/arm/mm/proc-v7-bugs.c b/arch/arm/mm/proc-v7-bugs.c index 5544b82a2e7a..9a07916af8dd 100644 --- a/arch/arm/mm/proc-v7-bugs.c +++ b/arch/arm/mm/proc-v7-bugs.c @@ -52,8 +52,6 @@ static void cpu_v7_spectre_init(void) case ARM_CPU_PART_CORTEX_A17: case ARM_CPU_PART_CORTEX_A73: case ARM_CPU_PART_CORTEX_A75: - if (processor.switch_mm != cpu_v7_bpiall_switch_mm) - goto bl_error; per_cpu(harden_branch_predictor_fn, cpu) = harden_branch_predictor_bpiall; spectre_v2_method = "BPIALL"; @@ -61,8 +59,6 @@ static void cpu_v7_spectre_init(void) case ARM_CPU_PART_CORTEX_A15: case ARM_CPU_PART_BRAHMA_B15: - if (processor.switch_mm != cpu_v7_iciallu_switch_mm) - goto bl_error; per_cpu(harden_branch_predictor_fn, cpu) = harden_branch_predictor_iciallu; spectre_v2_method = "ICIALLU"; @@ -88,11 +84,9 @@ static void cpu_v7_spectre_init(void) ARM_SMCCC_ARCH_WORKAROUND_1, &res); if ((int)res.a0 != 0) break; - if (processor.switch_mm != cpu_v7_hvc_switch_mm && cpu) - goto bl_error; per_cpu(harden_branch_predictor_fn, cpu) = call_hvc_arch_workaround_1; - processor.switch_mm = cpu_v7_hvc_switch_mm; + cpu_do_switch_mm = cpu_v7_hvc_switch_mm; spectre_v2_method = "hypervisor"; break; @@ -101,11 +95,9 @@ static void cpu_v7_spectre_init(void) ARM_SMCCC_ARCH_WORKAROUND_1, &res); if ((int)res.a0 != 0) break; - if (processor.switch_mm != cpu_v7_smc_switch_mm && cpu) - goto bl_error; per_cpu(harden_branch_predictor_fn, cpu) = call_smc_arch_workaround_1; - processor.switch_mm = cpu_v7_smc_switch_mm; + cpu_do_switch_mm = cpu_v7_smc_switch_mm; spectre_v2_method = "firmware"; break; @@ -119,11 +111,6 @@ static void cpu_v7_spectre_init(void) if (spectre_v2_method) pr_info("CPU%u: Spectre v2: using %s workaround\n", smp_processor_id(), spectre_v2_method); - return; - -bl_error: - pr_err("CPU%u: Spectre v2: incorrect context switching function, system vulnerable\n", - cpu); } #else static void cpu_v7_spectre_init(void)