Skip to content

Commit 57b8e2d

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm updates from Paolo Bonzini: "On top of a lot of Arm fixes, this includes a massive rename of types and variables in tools/testing/selftests/kvm - these were unnecessarily different from what the kernel uses, so they're being made consistent. arm64: - Allow tracing for non-pKVM, which was accidentally disabled when the series was merged - Rationalise the way the pKVM hypercall ranges are defined by using the same mechanism as already used for the vcpu_sysreg enum - Enforce that SMCCC function numbers relayed by the pKVM proxy are actually compliant with the specification - Fix a couple of feature to idreg mappings which resulted in the wrong sanitisation being applied - Fix the GICD_IIDR revision number field that could never been written correctly by userspace - Make kvm_vcpu_initialized() correctly use its parameter instead of relying on the surrounding context - Enforce correct ordering in __pkvm_init_vcpu(), plugging a potential pin leak at the same time - Move __pkvm_init_finalise() to a less dangerous spot, avoiding future problems - Restore functional userspace irqchip support after a four year breakage (last functional kernel was 5.18...) - Spelling fixes Selftests: - Rename types across all KVM selftests to more closely align with types used in the kernel: vm_vaddr_t -> gva_t vm_paddr_t -> gpa_t uint64_t -> u64 uint32_t -> u32 uint16_t -> u16 uint8_t -> u8 int64_t -> s64 int32_t -> s32 int16_t -> s16 int8_t -> s8 - Fix Loongarch compilation" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (31 commits) KVM: selftests: Add check_steal_time_uapi() implementation for LoongArch KVM: arm64: Wake-up from WFI when iqrchip is in userspace KVM: arm64: Fix initialisation order in __pkvm_init_finalise() KVM: arm64: Fix pin leak and publication ordering in __pkvm_init_vcpu() KVM: arm64: Fix kvm_vcpu_initialized() macro parameter KVM: arm64: Fix FEAT_SPE_FnE to use PMSIDR_EL1.FnE, not PMSVer KVM: arm64: Fix typo in feature check comments KVM: arm64: Fix FEAT_Debugv8p9 to check DebugVer, not PMUVer KVM: arm64: Reject non compliant SMCCC function calls in pKVM KVM: arm64: vgic: Fix IIDR revision field extracted from wrong value KVM: selftests: Replace "paddr" with "gpa" throughout KVM: selftests: Replace "u64 nested_paddr" with "gpa_t l2_gpa" KVM: selftests: Replace "u64 gpa" with "gpa_t" throughout KVM: selftests: Replace "vaddr" with "gva" throughout KVM: selftests: Clarify that arm64's inject_uer() takes a host PA, not a guest PA KVM: selftests: Rename translate_to_host_paddr() => translate_hva_to_hpa() KVM: selftests: Rename vm_vaddr_populate_bitmap() => vm_populate_gva_bitmap() KVM: selftests: Rename vm_vaddr_unused_gap() => vm_unused_gva_gap() KVM: selftests: Drop "vaddr_" from APIs that allocate memory for a given VM KVM: selftests: Use u8 instead of uint8_t ...
2 parents 664f0f6 + 39f1c20 commit 57b8e2d

194 files changed

Lines changed: 2800 additions & 2872 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

arch/arm64/include/asm/kvm_asm.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050

5151
#include <linux/mm.h>
5252

53+
#define MARKER(m) \
54+
m, __after_##m = m - 1
55+
5356
enum __kvm_host_smccc_func {
5457
/* Hypercalls that are unavailable once pKVM has finalised. */
5558
/* __KVM_HOST_SMCCC_FUNC___kvm_hyp_init */
@@ -59,8 +62,10 @@ enum __kvm_host_smccc_func {
5962
__KVM_HOST_SMCCC_FUNC___kvm_enable_ssbs,
6063
__KVM_HOST_SMCCC_FUNC___vgic_v3_init_lrs,
6164
__KVM_HOST_SMCCC_FUNC___vgic_v3_get_gic_config,
65+
66+
MARKER(__KVM_HOST_SMCCC_FUNC_MIN_PKVM),
67+
6268
__KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize,
63-
__KVM_HOST_SMCCC_FUNC_MIN_PKVM = __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize,
6469

6570
/* Hypercalls that are always available and common to [nh]VHE/pKVM. */
6671
__KVM_HOST_SMCCC_FUNC___kvm_adjust_pc,
@@ -72,11 +77,20 @@ enum __kvm_host_smccc_func {
7277
__KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_range,
7378
__KVM_HOST_SMCCC_FUNC___kvm_flush_cpu_context,
7479
__KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntvoff,
80+
__KVM_HOST_SMCCC_FUNC___tracing_load,
81+
__KVM_HOST_SMCCC_FUNC___tracing_unload,
82+
__KVM_HOST_SMCCC_FUNC___tracing_enable,
83+
__KVM_HOST_SMCCC_FUNC___tracing_swap_reader,
84+
__KVM_HOST_SMCCC_FUNC___tracing_update_clock,
85+
__KVM_HOST_SMCCC_FUNC___tracing_reset,
86+
__KVM_HOST_SMCCC_FUNC___tracing_enable_event,
87+
__KVM_HOST_SMCCC_FUNC___tracing_write_event,
7588
__KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs,
7689
__KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs,
7790
__KVM_HOST_SMCCC_FUNC___vgic_v5_save_apr,
7891
__KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr,
79-
__KVM_HOST_SMCCC_FUNC_MAX_NO_PKVM = __KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr,
92+
93+
MARKER(__KVM_HOST_SMCCC_FUNC_PKVM_ONLY),
8094

8195
/* Hypercalls that are available only when pKVM has finalised. */
8296
__KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp,
@@ -100,14 +114,8 @@ enum __kvm_host_smccc_func {
100114
__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load,
101115
__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put,
102116
__KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid,
103-
__KVM_HOST_SMCCC_FUNC___tracing_load,
104-
__KVM_HOST_SMCCC_FUNC___tracing_unload,
105-
__KVM_HOST_SMCCC_FUNC___tracing_enable,
106-
__KVM_HOST_SMCCC_FUNC___tracing_swap_reader,
107-
__KVM_HOST_SMCCC_FUNC___tracing_update_clock,
108-
__KVM_HOST_SMCCC_FUNC___tracing_reset,
109-
__KVM_HOST_SMCCC_FUNC___tracing_enable_event,
110-
__KVM_HOST_SMCCC_FUNC___tracing_write_event,
117+
118+
MARKER(__KVM_HOST_SMCCC_FUNC_MAX)
111119
};
112120

113121
#define DECLARE_KVM_VHE_SYM(sym) extern char sym[]

arch/arm64/include/asm/kvm_host.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,6 @@ struct kvm_vcpu_fault_info {
450450
r = __VNCR_START__ + ((VNCR_ ## r) / 8), \
451451
__after_##r = __MAX__(__before_##r - 1, r)
452452

453-
#define MARKER(m) \
454-
m, __after_##m = m - 1
455-
456453
enum vcpu_sysreg {
457454
__INVALID_SYSREG__, /* 0 is reserved as an invalid value */
458455
MPIDR_EL1, /* MultiProcessor Affinity Register */
@@ -1548,7 +1545,7 @@ static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
15481545
#define kvm_vcpu_has_feature(k, f) __vcpu_has_feature(&(k)->arch, (f))
15491546
#define vcpu_has_feature(v, f) __vcpu_has_feature(&(v)->kvm->arch, (f))
15501547

1551-
#define kvm_vcpu_initialized(v) vcpu_get_flag(vcpu, VCPU_INITIALIZED)
1548+
#define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED)
15521549

15531550
int kvm_trng_call(struct kvm_vcpu *vcpu);
15541551
#ifdef CONFIG_KVM

arch/arm64/kvm/arm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,10 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
824824
{
825825
bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF | HCR_VSE);
826826

827+
irq_lines |= (!irqchip_in_kernel(v->kvm) &&
828+
(kvm_timer_should_notify_user(v) ||
829+
kvm_pmu_should_notify_user(v)));
830+
827831
return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
828832
&& !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
829833
}

arch/arm64/kvm/config.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ struct reg_feat_map_desc {
131131
}
132132

133133
#define FEAT_SPE ID_AA64DFR0_EL1, PMSVer, IMP
134-
#define FEAT_SPE_FnE ID_AA64DFR0_EL1, PMSVer, V1P2
135134
#define FEAT_BRBE ID_AA64DFR0_EL1, BRBE, IMP
136135
#define FEAT_TRC_SR ID_AA64DFR0_EL1, TraceVer, IMP
137136
#define FEAT_PMUv3 ID_AA64DFR0_EL1, PMUVer, IMP
@@ -192,7 +191,7 @@ struct reg_feat_map_desc {
192191
#define FEAT_SRMASK ID_AA64MMFR4_EL1, SRMASK, IMP
193192
#define FEAT_PoPS ID_AA64MMFR4_EL1, PoPS, IMP
194193
#define FEAT_PFAR ID_AA64PFR1_EL1, PFAR, IMP
195-
#define FEAT_Debugv8p9 ID_AA64DFR0_EL1, PMUVer, V3P9
194+
#define FEAT_Debugv8p9 ID_AA64DFR0_EL1, DebugVer, V8P9
196195
#define FEAT_PMUv3_SS ID_AA64DFR0_EL1, PMSS, IMP
197196
#define FEAT_SEBEP ID_AA64DFR0_EL1, SEBEP, IMP
198197
#define FEAT_EBEP ID_AA64DFR1_EL1, EBEP, IMP
@@ -283,7 +282,7 @@ static bool feat_anerr(struct kvm *kvm)
283282
static bool feat_sme_smps(struct kvm *kvm)
284283
{
285284
/*
286-
* Revists this if KVM ever supports SME -- this really should
285+
* Revisit this if KVM ever supports SME -- this really should
287286
* look at the guest's view of SMIDR_EL1. Funnily enough, this
288287
* is not captured in the JSON file, but only as a note in the
289288
* ARM ARM.
@@ -295,17 +294,27 @@ static bool feat_sme_smps(struct kvm *kvm)
295294
static bool feat_spe_fds(struct kvm *kvm)
296295
{
297296
/*
298-
* Revists this if KVM ever supports SPE -- this really should
297+
* Revisit this if KVM ever supports SPE -- this really should
299298
* look at the guest's view of PMSIDR_EL1.
300299
*/
301300
return (kvm_has_feat(kvm, FEAT_SPEv1p4) &&
302301
(read_sysreg_s(SYS_PMSIDR_EL1) & PMSIDR_EL1_FDS));
303302
}
304303

304+
static bool feat_spe_fne(struct kvm *kvm)
305+
{
306+
/*
307+
* Revisit this if KVM ever supports SPE -- this really should
308+
* look at the guest's view of PMSIDR_EL1.
309+
*/
310+
return (kvm_has_feat(kvm, FEAT_SPEv1p2) &&
311+
(read_sysreg_s(SYS_PMSIDR_EL1) & PMSIDR_EL1_FnE));
312+
}
313+
305314
static bool feat_trbe_mpam(struct kvm *kvm)
306315
{
307316
/*
308-
* Revists this if KVM ever supports both MPAM and TRBE --
317+
* Revisit this if KVM ever supports both MPAM and TRBE --
309318
* this really should look at the guest's view of TRBIDR_EL1.
310319
*/
311320
return (kvm_has_feat(kvm, FEAT_TRBE) &&
@@ -537,7 +546,7 @@ static const struct reg_bits_to_feat_map hdfgrtr_feat_map[] = {
537546
HDFGRTR_EL2_PMBPTR_EL1 |
538547
HDFGRTR_EL2_PMBLIMITR_EL1,
539548
FEAT_SPE),
540-
NEEDS_FEAT(HDFGRTR_EL2_nPMSNEVFR_EL1, FEAT_SPE_FnE),
549+
NEEDS_FEAT(HDFGRTR_EL2_nPMSNEVFR_EL1, feat_spe_fne),
541550
NEEDS_FEAT(HDFGRTR_EL2_nBRBDATA |
542551
HDFGRTR_EL2_nBRBCTL |
543552
HDFGRTR_EL2_nBRBIDR,
@@ -605,7 +614,7 @@ static const struct reg_bits_to_feat_map hdfgwtr_feat_map[] = {
605614
HDFGWTR_EL2_PMBPTR_EL1 |
606615
HDFGWTR_EL2_PMBLIMITR_EL1,
607616
FEAT_SPE),
608-
NEEDS_FEAT(HDFGWTR_EL2_nPMSNEVFR_EL1, FEAT_SPE_FnE),
617+
NEEDS_FEAT(HDFGWTR_EL2_nPMSNEVFR_EL1, feat_spe_fne),
609618
NEEDS_FEAT(HDFGWTR_EL2_nBRBDATA |
610619
HDFGWTR_EL2_nBRBCTL,
611620
FEAT_BRBE),

arch/arm64/kvm/hyp/nvhe/hyp-main.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,14 @@ static const hcall_t host_hcall[] = {
709709
HANDLE_FUNC(__kvm_tlb_flush_vmid_range),
710710
HANDLE_FUNC(__kvm_flush_cpu_context),
711711
HANDLE_FUNC(__kvm_timer_set_cntvoff),
712+
HANDLE_FUNC(__tracing_load),
713+
HANDLE_FUNC(__tracing_unload),
714+
HANDLE_FUNC(__tracing_enable),
715+
HANDLE_FUNC(__tracing_swap_reader),
716+
HANDLE_FUNC(__tracing_update_clock),
717+
HANDLE_FUNC(__tracing_reset),
718+
HANDLE_FUNC(__tracing_enable_event),
719+
HANDLE_FUNC(__tracing_write_event),
712720
HANDLE_FUNC(__vgic_v3_save_aprs),
713721
HANDLE_FUNC(__vgic_v3_restore_vmcr_aprs),
714722
HANDLE_FUNC(__vgic_v5_save_apr),
@@ -735,22 +743,16 @@ static const hcall_t host_hcall[] = {
735743
HANDLE_FUNC(__pkvm_vcpu_load),
736744
HANDLE_FUNC(__pkvm_vcpu_put),
737745
HANDLE_FUNC(__pkvm_tlb_flush_vmid),
738-
HANDLE_FUNC(__tracing_load),
739-
HANDLE_FUNC(__tracing_unload),
740-
HANDLE_FUNC(__tracing_enable),
741-
HANDLE_FUNC(__tracing_swap_reader),
742-
HANDLE_FUNC(__tracing_update_clock),
743-
HANDLE_FUNC(__tracing_reset),
744-
HANDLE_FUNC(__tracing_enable_event),
745-
HANDLE_FUNC(__tracing_write_event),
746746
};
747747

748748
static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
749749
{
750750
DECLARE_REG(unsigned long, id, host_ctxt, 0);
751-
unsigned long hcall_min = 0, hcall_max = -1;
751+
unsigned long hcall_min = 0, hcall_max = __KVM_HOST_SMCCC_FUNC_MAX;
752752
hcall_t hfn;
753753

754+
BUILD_BUG_ON(ARRAY_SIZE(host_hcall) != __KVM_HOST_SMCCC_FUNC_MAX);
755+
754756
/*
755757
* If pKVM has been initialised then reject any calls to the
756758
* early "privileged" hypercalls. Note that we cannot reject
@@ -763,16 +765,14 @@ static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
763765
if (static_branch_unlikely(&kvm_protected_mode_initialized)) {
764766
hcall_min = __KVM_HOST_SMCCC_FUNC_MIN_PKVM;
765767
} else {
766-
hcall_max = __KVM_HOST_SMCCC_FUNC_MAX_NO_PKVM;
768+
hcall_max = __KVM_HOST_SMCCC_FUNC_PKVM_ONLY;
767769
}
768770

769771
id &= ~ARM_SMCCC_CALL_HINTS;
770772
id -= KVM_HOST_SMCCC_ID(0);
771773

772-
if (unlikely(id < hcall_min || id > hcall_max ||
773-
id >= ARRAY_SIZE(host_hcall))) {
774+
if (unlikely(id < hcall_min || id >= hcall_max))
774775
goto inval;
775-
}
776776

777777
hfn = host_hcall[id];
778778
if (unlikely(!hfn))
@@ -805,6 +805,10 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
805805
}
806806

807807
func_id &= ~ARM_SMCCC_CALL_HINTS;
808+
if (upper_32_bits(func_id)) {
809+
cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
810+
goto exit_skip_instr;
811+
}
808812

809813
handled = kvm_host_psci_handler(host_ctxt, func_id);
810814
if (!handled)

arch/arm64/kvm/hyp/nvhe/pkvm.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle,
266266
if (hyp_vm->kvm.created_vcpus <= vcpu_idx)
267267
goto unlock;
268268

269-
hyp_vcpu = hyp_vm->vcpus[vcpu_idx];
269+
/* Pairs with smp_store_release() in register_hyp_vcpu(). */
270+
hyp_vcpu = smp_load_acquire(&hyp_vm->vcpus[vcpu_idx]);
270271
if (!hyp_vcpu)
271272
goto unlock;
272273

@@ -860,12 +861,30 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
860861
* the page-aligned size of 'struct pkvm_hyp_vcpu'.
861862
* Return 0 on success, negative error code on failure.
862863
*/
864+
static int register_hyp_vcpu(struct pkvm_hyp_vm *hyp_vm,
865+
struct pkvm_hyp_vcpu *hyp_vcpu)
866+
{
867+
unsigned int idx = hyp_vcpu->vcpu.vcpu_idx;
868+
869+
if (idx >= hyp_vm->kvm.created_vcpus)
870+
return -EINVAL;
871+
872+
if (hyp_vm->vcpus[idx])
873+
return -EINVAL;
874+
875+
/*
876+
* Ensure the hyp_vcpu is initialised before publishing it to
877+
* the vCPU-load path via 'hyp_vm->vcpus[]'.
878+
*/
879+
smp_store_release(&hyp_vm->vcpus[idx], hyp_vcpu);
880+
return 0;
881+
}
882+
863883
int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
864884
unsigned long vcpu_hva)
865885
{
866886
struct pkvm_hyp_vcpu *hyp_vcpu;
867887
struct pkvm_hyp_vm *hyp_vm;
868-
unsigned int idx;
869888
int ret;
870889

871890
hyp_vcpu = map_donated_memory(vcpu_hva, sizeof(*hyp_vcpu));
@@ -884,18 +903,11 @@ int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
884903
if (ret)
885904
goto unlock;
886905

887-
idx = hyp_vcpu->vcpu.vcpu_idx;
888-
if (idx >= hyp_vm->kvm.created_vcpus) {
889-
ret = -EINVAL;
890-
goto unlock;
891-
}
892-
893-
if (hyp_vm->vcpus[idx]) {
894-
ret = -EINVAL;
895-
goto unlock;
906+
ret = register_hyp_vcpu(hyp_vm, hyp_vcpu);
907+
if (ret) {
908+
unpin_host_vcpu(host_vcpu);
909+
unpin_host_sve_state(hyp_vcpu);
896910
}
897-
898-
hyp_vm->vcpus[idx] = hyp_vcpu;
899911
unlock:
900912
hyp_spin_unlock(&vm_table_lock);
901913

arch/arm64/kvm/hyp/nvhe/setup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ void __noreturn __pkvm_init_finalise(void)
312312
};
313313
pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops;
314314

315-
ret = fix_host_ownership();
315+
ret = fix_hyp_pgtable_refcnt();
316316
if (ret)
317317
goto out;
318318

319-
ret = fix_hyp_pgtable_refcnt();
319+
ret = hyp_create_fixmap();
320320
if (ret)
321321
goto out;
322322

323-
ret = hyp_create_fixmap();
323+
ret = fix_host_ownership();
324324
if (ret)
325325
goto out;
326326

arch/arm64/kvm/vgic/vgic-mmio-v2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static int vgic_mmio_uaccess_write_v2_misc(struct kvm_vcpu *vcpu,
9191
* migration from old kernels to new kernels with legacy
9292
* userspace.
9393
*/
94-
reg = FIELD_GET(GICD_IIDR_REVISION_MASK, reg);
94+
reg = FIELD_GET(GICD_IIDR_REVISION_MASK, val);
9595
switch (reg) {
9696
case KVM_VGIC_IMP_REV_2:
9797
case KVM_VGIC_IMP_REV_3:

arch/arm64/kvm/vgic/vgic-mmio-v3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
194194
if ((reg ^ val) & ~GICD_IIDR_REVISION_MASK)
195195
return -EINVAL;
196196

197-
reg = FIELD_GET(GICD_IIDR_REVISION_MASK, reg);
197+
reg = FIELD_GET(GICD_IIDR_REVISION_MASK, val);
198198
switch (reg) {
199199
case KVM_VGIC_IMP_REV_2:
200200
case KVM_VGIC_IMP_REV_3:

0 commit comments

Comments
 (0)