Skip to content

Commit d86de40

Browse files
mrutland-armMarc Zyngier
authored andcommitted
arm64: cpufeature: upgrade hyp caps to final
We finalize caps before initializing kvm hyp code, and any use of cpus_have_const_cap() in kvm hyp code generates redundant and potentially unsound code to read the cpu_hwcaps array. A number of helper functions used in both hyp context and regular kernel context use cpus_have_const_cap(), as some regular kernel code runs before the capabilities are finalized. It's tedious and error-prone to write separate copies of these for hyp and non-hyp code. So that we can avoid the redundant code, let's automatically upgrade cpus_have_const_cap() to cpus_have_final_cap() when used in hyp context. With this change, there's never a reason to access to cpu_hwcaps array from hyp code, and we don't need to create an NVHE alias for this. This should have no effect on non-hyp code. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Acked-by: Will Deacon <will@kernel.org> Cc: David Brazdil <dbrazdil@google.com> Cc: Marc Zyngier <maz@kernel.org> Cc: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20201026134931.28246-4-mark.rutland@arm.com
1 parent dfc4e3f commit d86de40

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

arch/arm64/include/asm/cpufeature.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,23 @@ cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry,
375375
return false;
376376
}
377377

378+
static __always_inline bool is_vhe_hyp_code(void)
379+
{
380+
/* Only defined for code run in VHE hyp context */
381+
return __is_defined(__KVM_VHE_HYPERVISOR__);
382+
}
383+
384+
static __always_inline bool is_nvhe_hyp_code(void)
385+
{
386+
/* Only defined for code run in NVHE hyp context */
387+
return __is_defined(__KVM_NVHE_HYPERVISOR__);
388+
}
389+
390+
static __always_inline bool is_hyp_code(void)
391+
{
392+
return is_vhe_hyp_code() || is_nvhe_hyp_code();
393+
}
394+
378395
extern DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
379396
extern struct static_key_false cpu_hwcap_keys[ARM64_NCAPS];
380397
extern struct static_key_false arm64_const_caps_ready;
@@ -444,16 +461,21 @@ static __always_inline bool cpus_have_final_cap(int num)
444461
}
445462

446463
/*
447-
* Test for a capability, possibly with a runtime check.
464+
* Test for a capability, possibly with a runtime check for non-hyp code.
448465
*
466+
* For hyp code, this behaves the same as cpus_have_final_cap().
467+
*
468+
* For non-hyp code:
449469
* Before capabilities are finalized, this behaves as cpus_have_cap().
450470
* After capabilities are finalized, this is patched to avoid a runtime check.
451471
*
452472
* @num must be a compile-time constant.
453473
*/
454474
static __always_inline bool cpus_have_const_cap(int num)
455475
{
456-
if (system_capabilities_finalized())
476+
if (is_hyp_code())
477+
return cpus_have_final_cap(num);
478+
else if (system_capabilities_finalized())
457479
return __cpus_have_const_cap(num);
458480
else
459481
return cpus_have_cap(num);

arch/arm64/include/asm/virt.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,6 @@ static inline bool is_kernel_in_hyp_mode(void)
8383
return read_sysreg(CurrentEL) == CurrentEL_EL2;
8484
}
8585

86-
static __always_inline bool is_vhe_hyp_code(void)
87-
{
88-
/* Only defined for code run in VHE hyp context */
89-
return __is_defined(__KVM_VHE_HYPERVISOR__);
90-
}
91-
92-
static __always_inline bool is_nvhe_hyp_code(void)
93-
{
94-
/* Only defined for code run in NVHE hyp context */
95-
return __is_defined(__KVM_NVHE_HYPERVISOR__);
96-
}
97-
9886
static __always_inline bool has_vhe(void)
9987
{
10088
/*

arch/arm64/kernel/image-vars.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ KVM_NVHE_ALIAS(__icache_flags);
8787
/* Kernel symbols needed for cpus_have_final/const_caps checks. */
8888
KVM_NVHE_ALIAS(arm64_const_caps_ready);
8989
KVM_NVHE_ALIAS(cpu_hwcap_keys);
90-
KVM_NVHE_ALIAS(cpu_hwcaps);
9190

9291
/* Static keys which are set if a vGIC trap should be handled in hyp. */
9392
KVM_NVHE_ALIAS(vgic_v2_cpuif_trap);

0 commit comments

Comments
 (0)