Skip to content

Commit 4563243

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fixes from Paolo Bonzini: "ARM64: - Fix pKVM error path on init, making sure we do not change critical system registers as we're about to fail - Make sure that the host's vector length is at capped by a value common to all CPUs - Fix kvm_has_feat*() handling of "negative" features, as the current code is pretty broken - Promote Joey to the status of official reviewer, while James steps down -- hopefully only temporarly x86: - Fix compilation with KVM_INTEL=KVM_AMD=n - Fix disabling KVM_X86_QUIRK_SLOT_ZAP_ALL when shadow MMU is in use Selftests: - Fix compilation on non-x86 architectures" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: x86/reboot: emergency callbacks are now registered by common KVM code KVM: x86: leave kvm.ko out of the build if no vendor module is requested KVM: x86/mmu: fix KVM_X86_QUIRK_SLOT_ZAP_ALL for shadow MMU KVM: arm64: Fix kvm_has_feat*() handling of negative features KVM: selftests: Fix build on architectures other than x86_64 KVM: arm64: Another reviewer reshuffle KVM: arm64: Constrain the host to the maximum shared SVE VL with pKVM KVM: arm64: Fix __pkvm_init_vcpu cptr_el2 error path
2 parents b3ce5c3 + c8d430d commit 4563243

File tree

12 files changed

+91
-43
lines changed

12 files changed

+91
-43
lines changed

MAINTAINERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12463,7 +12463,7 @@ F: virt/kvm/*
1246312463
KERNEL VIRTUAL MACHINE FOR ARM64 (KVM/arm64)
1246412464
M: Marc Zyngier <[email protected]>
1246512465
M: Oliver Upton <[email protected]>
12466-
R: James Morse <james.morse@arm.com>
12466+
R: Joey Gouly <joey.gouly@arm.com>
1246712467
R: Suzuki K Poulose <[email protected]>
1246812468
R: Zenghui Yu <[email protected]>
1246912469
L: [email protected] (moderated for non-subscribers)

arch/arm64/include/asm/kvm_host.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,11 +1441,6 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
14411441
sign_extend64(__val, id##_##fld##_WIDTH - 1); \
14421442
})
14431443

1444-
#define expand_field_sign(id, fld, val) \
1445-
(id##_##fld##_SIGNED ? \
1446-
__expand_field_sign_signed(id, fld, val) : \
1447-
__expand_field_sign_unsigned(id, fld, val))
1448-
14491444
#define get_idreg_field_unsigned(kvm, id, fld) \
14501445
({ \
14511446
u64 __val = kvm_read_vm_id_reg((kvm), SYS_##id); \
@@ -1461,20 +1456,26 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
14611456
#define get_idreg_field_enum(kvm, id, fld) \
14621457
get_idreg_field_unsigned(kvm, id, fld)
14631458

1464-
#define get_idreg_field(kvm, id, fld) \
1459+
#define kvm_cmp_feat_signed(kvm, id, fld, op, limit) \
1460+
(get_idreg_field_signed((kvm), id, fld) op __expand_field_sign_signed(id, fld, limit))
1461+
1462+
#define kvm_cmp_feat_unsigned(kvm, id, fld, op, limit) \
1463+
(get_idreg_field_unsigned((kvm), id, fld) op __expand_field_sign_unsigned(id, fld, limit))
1464+
1465+
#define kvm_cmp_feat(kvm, id, fld, op, limit) \
14651466
(id##_##fld##_SIGNED ? \
1466-
get_idreg_field_signed(kvm, id, fld) : \
1467-
get_idreg_field_unsigned(kvm, id, fld))
1467+
kvm_cmp_feat_signed(kvm, id, fld, op, limit) : \
1468+
kvm_cmp_feat_unsigned(kvm, id, fld, op, limit))
14681469

14691470
#define kvm_has_feat(kvm, id, fld, limit) \
1470-
(get_idreg_field((kvm), id, fld) >= expand_field_sign(id, fld, limit))
1471+
kvm_cmp_feat(kvm, id, fld, >=, limit)
14711472

14721473
#define kvm_has_feat_enum(kvm, id, fld, val) \
1473-
(get_idreg_field_unsigned((kvm), id, fld) == __expand_field_sign_unsigned(id, fld, val))
1474+
kvm_cmp_feat_unsigned(kvm, id, fld, ==, val)
14741475

14751476
#define kvm_has_feat_range(kvm, id, fld, min, max) \
1476-
(get_idreg_field((kvm), id, fld) >= expand_field_sign(id, fld, min) && \
1477-
get_idreg_field((kvm), id, fld) <= expand_field_sign(id, fld, max))
1477+
(kvm_cmp_feat(kvm, id, fld, >=, min) && \
1478+
kvm_cmp_feat(kvm, id, fld, <=, max))
14781479

14791480
/* Check for a given level of PAuth support */
14801481
#define kvm_has_pauth(k, l) \

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static inline void __hyp_sve_save_host(void)
338338
struct cpu_sve_state *sve_state = *host_data_ptr(sve_state);
339339

340340
sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR);
341-
write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
341+
write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
342342
__sve_save_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl),
343343
&sve_state->fpsr,
344344
true);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void __hyp_sve_save_guest(struct kvm_vcpu *vcpu)
3333
*/
3434
sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2);
3535
__sve_save_state(vcpu_sve_pffr(vcpu), &vcpu->arch.ctxt.fp_regs.fpsr, true);
36-
write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
36+
write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
3737
}
3838

3939
static void __hyp_sve_restore_host(void)
@@ -45,10 +45,11 @@ static void __hyp_sve_restore_host(void)
4545
* the host. The layout of the data when saving the sve state depends
4646
* on the VL, so use a consistent (i.e., the maximum) host VL.
4747
*
48-
* Setting ZCR_EL2 to ZCR_ELx_LEN_MASK sets the effective length
49-
* supported by the system (or limited at EL3).
48+
* Note that this constrains the PE to the maximum shared VL
49+
* that was discovered, if we wish to use larger VLs this will
50+
* need to be revisited.
5051
*/
51-
write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
52+
write_sysreg_s(sve_vq_from_vl(kvm_host_sve_max_vl) - 1, SYS_ZCR_EL2);
5253
__sve_restore_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl),
5354
&sve_state->fpsr,
5455
true);
@@ -488,7 +489,8 @@ void handle_trap(struct kvm_cpu_context *host_ctxt)
488489
case ESR_ELx_EC_SVE:
489490
cpacr_clear_set(0, CPACR_ELx_ZEN);
490491
isb();
491-
sve_cond_update_zcr_vq(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
492+
sve_cond_update_zcr_vq(sve_vq_from_vl(kvm_host_sve_max_vl) - 1,
493+
SYS_ZCR_EL2);
492494
break;
493495
case ESR_ELx_EC_IABT_LOW:
494496
case ESR_ELx_EC_DABT_LOW:

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,14 @@ int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
574574
unlock:
575575
hyp_spin_unlock(&vm_table_lock);
576576

577-
if (ret)
577+
if (ret) {
578578
unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
579+
return ret;
580+
}
579581

580582
hyp_vcpu->vcpu.arch.cptr_el2 = kvm_get_reset_cptr_el2(&hyp_vcpu->vcpu);
581583

582-
return ret;
584+
return 0;
583585
}
584586

585587
static void

arch/x86/include/asm/reboot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ void __noreturn machine_real_restart(unsigned int type);
2626
#define MRR_APM 1
2727

2828
typedef void (cpu_emergency_virt_cb)(void);
29-
#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD)
29+
#if IS_ENABLED(CONFIG_KVM_X86)
3030
void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback);
3131
void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback);
3232
void cpu_emergency_disable_virtualization(void);
3333
#else
3434
static inline void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback) {}
3535
static inline void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback) {}
3636
static inline void cpu_emergency_disable_virtualization(void) {}
37-
#endif /* CONFIG_KVM_INTEL || CONFIG_KVM_AMD */
37+
#endif /* CONFIG_KVM_X86 */
3838

3939
typedef void (*nmi_shootdown_cb)(int, struct pt_regs*);
4040
void nmi_shootdown_cpus(nmi_shootdown_cb callback);

arch/x86/kernel/reboot.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static inline void kb_wait(void)
530530

531531
static inline void nmi_shootdown_cpus_on_restart(void);
532532

533-
#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD)
533+
#if IS_ENABLED(CONFIG_KVM_X86)
534534
/* RCU-protected callback to disable virtualization prior to reboot. */
535535
static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback;
536536

@@ -600,7 +600,7 @@ static void emergency_reboot_disable_virtualization(void)
600600
}
601601
#else
602602
static void emergency_reboot_disable_virtualization(void) { }
603-
#endif /* CONFIG_KVM_INTEL || CONFIG_KVM_AMD */
603+
#endif /* CONFIG_KVM_X86 */
604604

605605
void __attribute__((weak)) mach_reboot_fixups(void)
606606
{

arch/x86/kvm/Kconfig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ menuconfig VIRTUALIZATION
1717

1818
if VIRTUALIZATION
1919

20-
config KVM
21-
tristate "Kernel-based Virtual Machine (KVM) support"
20+
config KVM_X86
21+
def_tristate KVM if KVM_INTEL || KVM_AMD
2222
depends on X86_LOCAL_APIC
2323
select KVM_COMMON
2424
select KVM_GENERIC_MMU_NOTIFIER
@@ -44,7 +44,11 @@ config KVM
4444
select HAVE_KVM_PM_NOTIFIER if PM
4545
select KVM_GENERIC_HARDWARE_ENABLING
4646
select KVM_GENERIC_PRE_FAULT_MEMORY
47+
select KVM_GENERIC_PRIVATE_MEM if KVM_SW_PROTECTED_VM
4748
select KVM_WERROR if WERROR
49+
50+
config KVM
51+
tristate "Kernel-based Virtual Machine (KVM) support"
4852
help
4953
Support hosting fully virtualized guest machines using hardware
5054
virtualization extensions. You will need a fairly recent
@@ -77,7 +81,6 @@ config KVM_SW_PROTECTED_VM
7781
bool "Enable support for KVM software-protected VMs"
7882
depends on EXPERT
7983
depends on KVM && X86_64
80-
select KVM_GENERIC_PRIVATE_MEM
8184
help
8285
Enable support for KVM software-protected VMs. Currently, software-
8386
protected VMs are purely a development and testing vehicle for

arch/x86/kvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ kvm-intel-y += vmx/vmx_onhyperv.o vmx/hyperv_evmcs.o
3232
kvm-amd-y += svm/svm_onhyperv.o
3333
endif
3434

35-
obj-$(CONFIG_KVM) += kvm.o
35+
obj-$(CONFIG_KVM_X86) += kvm.o
3636
obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
3737
obj-$(CONFIG_KVM_AMD) += kvm-amd.o
3838

arch/x86/kvm/mmu/mmu.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,10 +1884,14 @@ static bool sp_has_gptes(struct kvm_mmu_page *sp)
18841884
if (is_obsolete_sp((_kvm), (_sp))) { \
18851885
} else
18861886

1887-
#define for_each_gfn_valid_sp_with_gptes(_kvm, _sp, _gfn) \
1887+
#define for_each_gfn_valid_sp(_kvm, _sp, _gfn) \
18881888
for_each_valid_sp(_kvm, _sp, \
18891889
&(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \
1890-
if ((_sp)->gfn != (_gfn) || !sp_has_gptes(_sp)) {} else
1890+
if ((_sp)->gfn != (_gfn)) {} else
1891+
1892+
#define for_each_gfn_valid_sp_with_gptes(_kvm, _sp, _gfn) \
1893+
for_each_gfn_valid_sp(_kvm, _sp, _gfn) \
1894+
if (!sp_has_gptes(_sp)) {} else
18911895

18921896
static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
18931897
{
@@ -7047,26 +7051,54 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
70477051
kvm_mmu_zap_all(kvm);
70487052
}
70497053

7050-
/*
7051-
* Zapping leaf SPTEs with memslot range when a memslot is moved/deleted.
7052-
*
7053-
* Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst
7054-
* case scenario we'll have unused shadow pages lying around until they
7055-
* are recycled due to age or when the VM is destroyed.
7056-
*/
7057-
static void kvm_mmu_zap_memslot_leafs(struct kvm *kvm, struct kvm_memory_slot *slot)
7054+
static void kvm_mmu_zap_memslot_pages_and_flush(struct kvm *kvm,
7055+
struct kvm_memory_slot *slot,
7056+
bool flush)
7057+
{
7058+
LIST_HEAD(invalid_list);
7059+
unsigned long i;
7060+
7061+
if (list_empty(&kvm->arch.active_mmu_pages))
7062+
goto out_flush;
7063+
7064+
/*
7065+
* Since accounting information is stored in struct kvm_arch_memory_slot,
7066+
* shadow pages deletion (e.g. unaccount_shadowed()) requires that all
7067+
* gfns with a shadow page have a corresponding memslot. Do so before
7068+
* the memslot goes away.
7069+
*/
7070+
for (i = 0; i < slot->npages; i++) {
7071+
struct kvm_mmu_page *sp;
7072+
gfn_t gfn = slot->base_gfn + i;
7073+
7074+
for_each_gfn_valid_sp(kvm, sp, gfn)
7075+
kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
7076+
7077+
if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
7078+
kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
7079+
flush = false;
7080+
cond_resched_rwlock_write(&kvm->mmu_lock);
7081+
}
7082+
}
7083+
7084+
out_flush:
7085+
kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
7086+
}
7087+
7088+
static void kvm_mmu_zap_memslot(struct kvm *kvm,
7089+
struct kvm_memory_slot *slot)
70587090
{
70597091
struct kvm_gfn_range range = {
70607092
.slot = slot,
70617093
.start = slot->base_gfn,
70627094
.end = slot->base_gfn + slot->npages,
70637095
.may_block = true,
70647096
};
7097+
bool flush;
70657098

70667099
write_lock(&kvm->mmu_lock);
7067-
if (kvm_unmap_gfn_range(kvm, &range))
7068-
kvm_flush_remote_tlbs_memslot(kvm, slot);
7069-
7100+
flush = kvm_unmap_gfn_range(kvm, &range);
7101+
kvm_mmu_zap_memslot_pages_and_flush(kvm, slot, flush);
70707102
write_unlock(&kvm->mmu_lock);
70717103
}
70727104

@@ -7082,7 +7114,7 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
70827114
if (kvm_memslot_flush_zap_all(kvm))
70837115
kvm_mmu_zap_all_fast(kvm);
70847116
else
7085-
kvm_mmu_zap_memslot_leafs(kvm, slot);
7117+
kvm_mmu_zap_memslot(kvm, slot);
70867118
}
70877119

70887120
void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen)

tools/testing/selftests/kvm/memslot_modification_stress_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,14 @@ int main(int argc, char *argv[])
169169
case 'i':
170170
p.nr_iterations = atoi_positive("Number of iterations", optarg);
171171
break;
172+
#ifdef __x86_64__
172173
case 'q':
173174
p.disable_slot_zap_quirk = true;
174175

175176
TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
176177
KVM_X86_QUIRK_SLOT_ZAP_ALL);
177178
break;
179+
#endif
178180
case 'h':
179181
default:
180182
help(argv[0]);

tools/testing/selftests/kvm/memslot_perf_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "atomic bool is not lockless");
113113
static sem_t vcpu_ready;
114114

115115
static bool map_unmap_verify;
116+
#ifdef __x86_64__
116117
static bool disable_slot_zap_quirk;
118+
#endif
117119

118120
static bool verbose;
119121
#define pr_info_v(...) \
@@ -579,8 +581,10 @@ static bool test_memslot_move_prepare(struct vm_data *data,
579581
uint32_t guest_page_size = data->vm->page_size;
580582
uint64_t movesrcgpa, movetestgpa;
581583

584+
#ifdef __x86_64__
582585
if (disable_slot_zap_quirk)
583586
vm_enable_cap(data->vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);
587+
#endif
584588

585589
movesrcgpa = vm_slot2gpa(data, data->nslots - 1);
586590

@@ -971,11 +975,13 @@ static bool parse_args(int argc, char *argv[],
971975
case 'd':
972976
map_unmap_verify = true;
973977
break;
978+
#ifdef __x86_64__
974979
case 'q':
975980
disable_slot_zap_quirk = true;
976981
TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
977982
KVM_X86_QUIRK_SLOT_ZAP_ALL);
978983
break;
984+
#endif
979985
case 's':
980986
targs->nslots = atoi_paranoid(optarg);
981987
if (targs->nslots <= 1 && targs->nslots != -1) {

0 commit comments

Comments
 (0)