Skip to content

Commit 1db5cde

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: cpufeature: add cpus_have_final_cap()
When cpus_have_const_cap() was originally introduced it was intended to be safe in hyp context, where it is not safe to access the cpu_hwcaps array as cpus_have_cap() did. For more details see commit: a4023f6 ("arm64: Add hypervisor safe helper for checking constant capabilities") We then made use of cpus_have_const_cap() throughout the kernel. Subsequently, we had to defer updating the static_key associated with each capability in order to avoid lockdep complaints. To avoid breaking kernel-wide usage of cpus_have_const_cap(), this was updated to fall back to the cpu_hwcaps array if called before the static_keys were updated. As the kvm hyp code was only called later than this, the fallback is redundant but not functionally harmful. For more details, see commit: 63a1e1c ("arm64/cpufeature: don't use mutex in bringup path") Today we have more users of cpus_have_const_cap() which are only called once the relevant static keys are initialized, and it would be beneficial to avoid the redundant code. To that end, this patch adds a new cpus_have_final_cap(), helper which is intend to be used in code which is only run once capabilities have been finalized, and will never check the cpus_hwcap array. This helps the compiler to generate better code as it no longer needs to generate code to address and test the cpus_hwcap array. To help catch misuse, cpus_have_final_cap() will BUG() if called before capabilities are finalized. In hyp context, BUG() will result in a hyp panic, but the specific BUG() instance will not be identified in the usual way. Comments are added to the various cpus_have_*_cap() helpers to describe the constraints on when they can be used. For clarity cpus_have_cap() is moved above the other helpers. Similarly the helpers are updated to use system_capabilities_finalized() consistently, and this is made __always_inline as required by its new callers. Signed-off-by: Mark Rutland <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Reviewed-by: Suzuki K Poulose <[email protected]> Cc: Will Deacon <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent f8788d8 commit 1db5cde

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

arch/arm64/include/asm/cpufeature.h

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,29 +390,70 @@ unsigned long cpu_get_elf_hwcap2(void);
390390
#define cpu_set_named_feature(name) cpu_set_feature(cpu_feature(name))
391391
#define cpu_have_named_feature(name) cpu_have_feature(cpu_feature(name))
392392

393-
/* System capability check for constant caps */
394-
static __always_inline bool __cpus_have_const_cap(int num)
393+
static __always_inline bool system_capabilities_finalized(void)
395394
{
396-
if (num >= ARM64_NCAPS)
397-
return false;
398-
return static_branch_unlikely(&cpu_hwcap_keys[num]);
395+
return static_branch_likely(&arm64_const_caps_ready);
399396
}
400397

398+
/*
399+
* Test for a capability with a runtime check.
400+
*
401+
* Before the capability is detected, this returns false.
402+
*/
401403
static inline bool cpus_have_cap(unsigned int num)
402404
{
403405
if (num >= ARM64_NCAPS)
404406
return false;
405407
return test_bit(num, cpu_hwcaps);
406408
}
407409

410+
/*
411+
* Test for a capability without a runtime check.
412+
*
413+
* Before capabilities are finalized, this returns false.
414+
* After capabilities are finalized, this is patched to avoid a runtime check.
415+
*
416+
* @num must be a compile-time constant.
417+
*/
418+
static __always_inline bool __cpus_have_const_cap(int num)
419+
{
420+
if (num >= ARM64_NCAPS)
421+
return false;
422+
return static_branch_unlikely(&cpu_hwcap_keys[num]);
423+
}
424+
425+
/*
426+
* Test for a capability, possibly with a runtime check.
427+
*
428+
* Before capabilities are finalized, this behaves as cpus_have_cap().
429+
* After capabilities are finalized, this is patched to avoid a runtime check.
430+
*
431+
* @num must be a compile-time constant.
432+
*/
408433
static __always_inline bool cpus_have_const_cap(int num)
409434
{
410-
if (static_branch_likely(&arm64_const_caps_ready))
435+
if (system_capabilities_finalized())
411436
return __cpus_have_const_cap(num);
412437
else
413438
return cpus_have_cap(num);
414439
}
415440

441+
/*
442+
* Test for a capability without a runtime check.
443+
*
444+
* Before capabilities are finalized, this will BUG().
445+
* After capabilities are finalized, this is patched to avoid a runtime check.
446+
*
447+
* @num must be a compile-time constant.
448+
*/
449+
static __always_inline bool cpus_have_final_cap(int num)
450+
{
451+
if (system_capabilities_finalized())
452+
return __cpus_have_const_cap(num);
453+
else
454+
BUG();
455+
}
456+
416457
static inline void cpus_set_cap(unsigned int num)
417458
{
418459
if (num >= ARM64_NCAPS) {
@@ -613,11 +654,6 @@ static inline bool system_has_prio_mask_debugging(void)
613654
system_uses_irq_prio_masking();
614655
}
615656

616-
static inline bool system_capabilities_finalized(void)
617-
{
618-
return static_branch_likely(&arm64_const_caps_ready);
619-
}
620-
621657
#define ARM64_BP_HARDEN_UNKNOWN -1
622658
#define ARM64_BP_HARDEN_WA_NEEDED 0
623659
#define ARM64_BP_HARDEN_NOT_REQUIRED 1

0 commit comments

Comments
 (0)