From patchwork Fri May 12 17:50:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 99711 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp428029qge; Fri, 12 May 2017 10:51:11 -0700 (PDT) X-Received: by 10.99.50.66 with SMTP id y63mr5584111pgy.41.1494611471765; Fri, 12 May 2017 10:51:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1494611471; cv=none; d=google.com; s=arc-20160816; b=VN2TsWK/jbreE8vbZC4iFOXmNj1Mtji/3bbSViA7d9vBFS+Qtde5HkIlkLaW9oUyfT qE+3F6ErVlqH9HBymIr4Ha4iMitrfNDlKN6mpxIeoqkPeS5EwYWlvRglZyZufBAfY1G4 9HlUyszjaq5Xu6ik7Fhj1V8qY0ZFEpD4cd79SHdAPr/DpNVPsLMe1w6vv1USQdz/ETCW 6R+/PxdaWzvsLL3Dy8UIqZb9GYOjzNfF9GunsCGyayEag6suMwXsuRKhfIjXE+i7LkIO TlZlC4Hmwi5xAKlDgq5qFDrjd3VrL2fyJY2wHJJal9IRVlBUfvG5cO2s5+GFfNlPi2W5 YRIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=RFjrwWF7wxbLigfoDraagveJomuxMnr/2Gx2zhvYi6Y=; b=f2zJTfqWcc07zJGJAA8S1MA6FOOuas/PDrmoc86n2bONQEhBXB/mkOsfiqOun4WoJ7 m6WgJYTJb4Y++B5NvGED/vlKViwWG/cGvFNhSYzsyPt8ksi9npCX7XP76RPamatTFFpu mY55+T6dWeSX7U1/57f5hnJ0LPVSOEwaR0dSvp+tNv76b8YAwQesEBxlopj42zYbFfaZ fBXcgpJI+qdsMjZ6yTmUXCTXWXSghwuYZlTAmVdA1V8uQsWMpL/Dp/tnn8rIZPcycvXN L+PxHEOWhsH29ZA0W3ze1qrOivM+n4NEITb89cORWV3wKWbzuAVxRteIHpAob6xQxy3S 2CPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 70si3702925pga.321.2017.05.12.10.51.11; Fri, 12 May 2017 10:51:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758415AbdELRu5 (ORCPT + 25 others); Fri, 12 May 2017 13:50:57 -0400 Received: from foss.arm.com ([217.140.101.70]:34330 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757496AbdELRu4 (ORCPT ); Fri, 12 May 2017 13:50:56 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8863880D; Fri, 12 May 2017 10:50:55 -0700 (PDT) Received: from leverpostej.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id B068E3F220; Fri, 12 May 2017 10:50:53 -0700 (PDT) From: Mark Rutland To: linux-arm-kernel@lists.infradead.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, bigeasy@linutronix.de, catalin.marinas@arm.com, marc.zyngier@arm.com, mark.rutland@arm.com, peterz@infradead.org, suzuki.poulose@arm.com, will.deacon@arm.com, Christoffer Dall Subject: [PATCHv4] arm64/cpufeature: don't use mutex in bringup path Date: Fri, 12 May 2017 18:50:18 +0100 Message-Id: <1494611418-14937-1-git-send-email-mark.rutland@arm.com> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, cpus_set_cap() calls static_branch_enable_cpuslocked(), which must take the jump_label mutex. We call cpus_set_cap() in the secondary bringup path, from the idle thread where interrupts are disabled. Taking a mutex in this path "is a NONO" regardless of whether it's contended, and something we must avoid. Additionally, the secondary CPU doesn't hold the percpu rwsem (as this is held by the primary CPU), so this triggers a lockdep splat. This patch fixes both issues. The poking of static keys is deferred until enable_cpu_capabilities(), which runs in a suitable context on the boot CPU. To account for the static keys being set later, cpus_have_const_cap() is updated to use another static key to check whether the const cap keys have been initialised, falling back to the caps bitmap until this is the case. This means that users of cpus_have_const_cap() gain should only gain a single additional NOP in the fast path once the const caps are initialised, but should always see the current cap value. The hyp code should never dereference the caps array, since the caps are initialized before we run the module initcall to initialise hyp. A check is added to the hyp init code to document this requirement. This rework means that we can remove the *_cpuslocked() helpers added in commit d54bb72551b999dd ("arm64/cpufeature: Use static_branch_enable_cpuslocked()"). Signed-off-by: Mark Rutland Reviewed-by: Marc Zyniger Reviewed-by: Suzuki Poulose Acked-by: Will Deacon Cc: Catalin Marinas Cc: Christoffer Dall Cc: Peter Zijlstra Cc: Sebastian Sewior Cc: Thomas Gleixner --- arch/arm64/include/asm/cpufeature.h | 13 ++++++++++--- arch/arm64/include/asm/kvm_host.h | 8 ++++++-- arch/arm64/kernel/cpu_errata.c | 9 +-------- arch/arm64/kernel/cpufeature.c | 25 ++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 16 deletions(-) Thomas, are you happy to pick this up? Will's happy for this to go via tip. I've checked that this builds and boots atop of the tip smp/hotplug branch. Thanks, Mark. Since v1 [1]: * Kill redundant update_cpu_errata_workarounds() prototype * Introduce arm64_const_caps_ready Since v2 [2]: * Add hyp init check * Clean up commit message * Drop fixes tag Since v3 [3]: * Fix typos * Accumulate tags [1] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-May/505731.html [2] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-May/505763.html [3] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-May/505887.html -- 1.9.1 diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index a8d1b5b..aba574c 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -115,6 +115,7 @@ struct arm64_cpu_capabilities { extern DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS); extern struct static_key_false cpu_hwcap_keys[ARM64_NCAPS]; +extern struct static_key_false arm64_const_caps_ready; bool this_cpu_has_cap(unsigned int cap); @@ -124,7 +125,7 @@ static inline bool cpu_have_feature(unsigned int num) } /* System capability check for constant caps */ -static inline bool cpus_have_const_cap(int num) +static inline bool __cpus_have_const_cap(int num) { if (num >= ARM64_NCAPS) return false; @@ -138,6 +139,14 @@ static inline bool cpus_have_cap(unsigned int num) return test_bit(num, cpu_hwcaps); } +static inline bool cpus_have_const_cap(int num) +{ + if (static_branch_likely(&arm64_const_caps_ready)) + return __cpus_have_const_cap(num); + else + return cpus_have_cap(num); +} + static inline void cpus_set_cap(unsigned int num) { if (num >= ARM64_NCAPS) { @@ -145,7 +154,6 @@ static inline void cpus_set_cap(unsigned int num) num, ARM64_NCAPS); } else { __set_bit(num, cpu_hwcaps); - static_branch_enable_cpuslocked(&cpu_hwcap_keys[num]); } } @@ -223,7 +231,6 @@ void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps, void check_local_cpu_capabilities(void); void update_cpu_errata_workarounds(void); -void update_cpu_errata_workarounds_cpuslocked(void); void __init enable_errata_workarounds(void); void verify_local_cpu_errata_workarounds(void); diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e7705e7..9973f0a 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -356,9 +357,12 @@ static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr, unsigned long vector_ptr) { /* - * Call initialization code, and switch to the full blown - * HYP code. + * Call initialization code, and switch to the full blown HYP code. + * If the cpucaps haven't been finalized yet, something has gone very + * wrong, and hyp will crash and burn when it uses any + * cpus_have_const_cap() wrapper. */ + BUG_ON(!static_branch_likely(&arm64_const_caps_ready)); __kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr); } diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index e650cf8..f6cc67e 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -175,16 +175,9 @@ void verify_local_cpu_errata_workarounds(void) } } -void update_cpu_errata_workarounds_cpuslocked(void) -{ - update_cpu_capabilities(arm64_errata, "enabling workaround for"); -} - void update_cpu_errata_workarounds(void) { - get_online_cpus(); - update_cpu_errata_workarounds_cpuslocked(); - put_online_cpus(); + update_cpu_capabilities(arm64_errata, "enabling workaround for"); } void __init enable_errata_workarounds(void) diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 113c06d..ebfd868 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -976,8 +976,16 @@ void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps, */ void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps) { - for (; caps->matches; caps++) - if (caps->enable && cpus_have_cap(caps->capability)) + for (; caps->matches; caps++) { + unsigned int num = caps->capability; + + if (!cpus_have_cap(num)) + continue; + + /* Ensure cpus_have_const_cap(num) works */ + static_branch_enable(&cpu_hwcap_keys[num]); + + if (caps->enable) { /* * Use stop_machine() as it schedules the work allowing * us to modify PSTATE, instead of on_each_cpu() which @@ -985,6 +993,8 @@ void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps) * we return. */ stop_machine(caps->enable, NULL, cpu_online_mask); + } + } } /* @@ -1076,7 +1086,7 @@ void check_local_cpu_capabilities(void) * advertised capabilities. */ if (!sys_caps_initialised) - update_cpu_errata_workarounds_cpuslocked(); + update_cpu_errata_workarounds(); else verify_local_cpu_capabilities(); } @@ -1089,6 +1099,14 @@ static void __init setup_feature_capabilities(void) enable_cpu_capabilities(arm64_features); } +DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready); +EXPORT_SYMBOL(arm64_const_caps_ready); + +static void __init mark_const_caps_ready(void) +{ + static_branch_enable(&arm64_const_caps_ready); +} + /* * Check if the current CPU has a given feature capability. * Should be called from non-preemptible context. @@ -1115,6 +1133,7 @@ void __init setup_cpu_features(void) /* Set the CPU feature capabilies */ setup_feature_capabilities(); enable_errata_workarounds(); + mark_const_caps_ready(); setup_elf_hwcaps(arm64_elf_hwcaps); if (system_supports_32bit_el0())