Skip to content

Commit 111903d

Browse files
author
Marc Zyngier
committed
KVM: arm64: nv: Hoist vcpu_has_nv() into is_hyp_ctxt()
A rather common idiom when writing NV code as part of KVM is to have things such has: if (vcpu_has_nv(vcpu) && is_hyp_ctxt(vcpu)) { [...] } to check that we are in a hyp-related context. The second part of the conjunction would be enough, but the first one contains a static key that allows the rest of the checkis to be elided when in a non-NV environment. Rewrite is_hyp_ctxt() to directly use vcpu_has_nv(). The result is the same, and the code easier to read. The one occurence of this that is already merged is rewritten in the process. In order to avoid nasty cirtular dependencies between kvm_emulate.h and kvm_nested.h, vcpu_has_feature() is itself hoisted into kvm_host.h, at the cost of some #deferry... Reviewed-by: Oliver Upton <oliver.upton@linux.dev> Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent 2bfc654 commit 111903d

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

arch/arm64/include/asm/kvm_emulate.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <asm/esr.h>
1818
#include <asm/kvm_arm.h>
1919
#include <asm/kvm_hyp.h>
20+
#include <asm/kvm_nested.h>
2021
#include <asm/ptrace.h>
2122
#include <asm/cputype.h>
2223
#include <asm/virt.h>
@@ -54,11 +55,6 @@ void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu);
5455
int kvm_inject_nested_sync(struct kvm_vcpu *vcpu, u64 esr_el2);
5556
int kvm_inject_nested_irq(struct kvm_vcpu *vcpu);
5657

57-
static inline bool vcpu_has_feature(const struct kvm_vcpu *vcpu, int feature)
58-
{
59-
return test_bit(feature, vcpu->kvm->arch.vcpu_features);
60-
}
61-
6258
#if defined(__KVM_VHE_HYPERVISOR__) || defined(__KVM_NVHE_HYPERVISOR__)
6359
static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
6460
{
@@ -248,7 +244,7 @@ static inline bool __is_hyp_ctxt(const struct kvm_cpu_context *ctxt)
248244

249245
static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
250246
{
251-
return __is_hyp_ctxt(&vcpu->arch.ctxt);
247+
return vcpu_has_nv(vcpu) && __is_hyp_ctxt(&vcpu->arch.ctxt);
252248
}
253249

254250
/*

arch/arm64/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,13 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
11771177
#define kvm_vm_has_ran_once(kvm) \
11781178
(test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags))
11791179

1180+
static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
1181+
{
1182+
return test_bit(feature, ka->vcpu_features);
1183+
}
1184+
1185+
#define vcpu_has_feature(v, f) __vcpu_has_feature(&(v)->kvm->arch, (f))
1186+
11801187
int kvm_trng_call(struct kvm_vcpu *vcpu);
11811188
#ifdef CONFIG_KVM
11821189
extern phys_addr_t hyp_mem_base;

arch/arm64/kvm/arch_timer.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ static u64 wfit_delay_ns(struct kvm_vcpu *vcpu)
295295
u64 val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
296296
struct arch_timer_context *ctx;
297297

298-
ctx = (vcpu_has_nv(vcpu) && is_hyp_ctxt(vcpu)) ? vcpu_hvtimer(vcpu)
299-
: vcpu_vtimer(vcpu);
298+
ctx = is_hyp_ctxt(vcpu) ? vcpu_hvtimer(vcpu) : vcpu_vtimer(vcpu);
300299

301300
return kvm_counter_compute_delta(ctx, val);
302301
}

0 commit comments

Comments
 (0)