From patchwork Tue May 16 14:18:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 99873 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp2045374qge; Tue, 16 May 2017 07:19:03 -0700 (PDT) X-Received: by 10.98.200.142 with SMTP id i14mr12697985pfk.154.1494944342871; Tue, 16 May 2017 07:19:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1494944342; cv=none; d=google.com; s=arc-20160816; b=WHodwnC7ppsGev+c6odjEaG4XqHv0FcILzil5t7rz7d4JTTzzoiXFKzubYOs67u/iI CxKAMhT2a6jbQtJT8y/nZ656pq+c5I3vy545IHrzcsTAO/5nPjcasXjpUCg8XUQhMzv0 +swGi16ljQQIM7y8+/46g99KmPI9ewuOhrXF3vqJt01sC/NC/Z38LqespTODPQINHMPu u6ZWfQpf2hg63yG6xtPB3RqEFSbDVTj7ATh57f3sMG8MDH6enJtK3hsbL88laC+xW2cB a5FC5B5Iov+2SXgFzSul9855v4rZ7PX/eT9OCwqJ44rlGGTk4uPTN6leef9a2Heu0GJL hcZw== 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=6eXVBpDR+ofUV5ql+eH7Of8OcnWUuePeV6biaUC8Pug=; b=zDDyXpt2s3bYM8bPoV6H7aMEKx0aPGOhk0EVZn+W472NgESzeKH3LyvYwxnyG9h3Wl rZUHsbIA8fNnsXVVlKXLc0uWAvaFBW0QKDQBbXHp3N183LqKgz7XKdQK9Xv+AR3mXahl Kr7wa2x6HuFhN8yzPy3MW7+dcrUiNT5A0TU0a5iKEIho2J5pmc2aP6bPyfI99FI8gC+p 2Of8OnXDObH+vPTfC/lHdSBt4phfGVlWSiJnxqB9QhSiGm7KnEKeHYo9+vOtShAjj+74 e8AKNrK5nWiwd8hj+AcR28w86FQVjHH3NiodqGU/UOBOLsaUhiUnvv4Lm+sCQzUiJXGd k2Rg== 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 1si13900104pgu.95.2017.05.16.07.19.02; Tue, 16 May 2017 07:19:02 -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 S1753513AbdEPOSv (ORCPT + 25 others); Tue, 16 May 2017 10:18:51 -0400 Received: from foss.arm.com ([217.140.101.70]:36752 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751649AbdEPOSt (ORCPT ); Tue, 16 May 2017 10:18:49 -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 E165B1435; Tue, 16 May 2017 07:18:48 -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 0E8283F3E1; Tue, 16 May 2017 07:18:46 -0700 (PDT) From: Mark Rutland To: linux-arm-kernel@lists.infradead.org, catalin.marinas@arm.com Cc: linux-kernel@vger.kernel.org, bigeasy@linutronix.de, marc.zyngier@arm.com, mark.rutland@arm.com, peterz@infradead.org, suzuki.poulose@arm.com, tglx@linutronix.de, will.deacon@arm.com, Christoffer Dall Subject: [PATCHv5] arm64/cpufeature: don't use mutex in bringup path Date: Tue, 16 May 2017 15:18:05 +0100 Message-Id: <1494944285-12152-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. We didn't spot this until recently, as ___might_sleep() won't warn for this case until all CPUs have been brought up. This patch avoids taking the mutex in the secondary bringup path. 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 change will sidestep a number of issues when the upcoming hotplug locking rework is merged. 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 | 12 ++++++++++-- arch/arm64/include/asm/kvm_host.h | 8 ++++++-- arch/arm64/kernel/cpufeature.c | 23 +++++++++++++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) Catalin, can you take this as a fix for v4.12? Thomas has zapped the tip smp/hotplug branch, so we won't see a conflict in linux-next with the prior attempt to clean this up. 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 Since v4 [4]: * Rebase to v4.12-rc1, so this can go via the arm64 tree * Clean up commit message [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 [4] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-May/505964.html -- 1.9.1 diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index e7f84a7..428ee1f 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(&cpu_hwcap_keys[num]); } } diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 5e19165..1f252a9 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 @@ -355,9 +356,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/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 94b8f7f..817ce33 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -985,8 +985,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 @@ -994,6 +1002,8 @@ void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps) * we return. */ stop_machine(caps->enable, NULL, cpu_online_mask); + } + } } /* @@ -1096,6 +1106,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. @@ -1131,6 +1149,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())