Skip to content

Commit fb184c4

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fixes from Paolo Bonzini: "The bigger part of the change is a revert for x86 hosts. Here the second patch was supposed to fix the first, but in reality it was just as broken, so both have to go. x86 host: - Revert incorrect assumption that cr3 changes come with preempt notifier callbacks (they don't when static branches are changed, for example) ARM host: - Correctly synchronise PMR and co on PSCI CPU_SUSPEND - Skip tests that depend on GICv3 when the HW isn't available" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: selftests: aarch64: Skip tests if we can't create a vgic-v3 Revert "KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()" Revert "KVM: VMX: Save HOST_CR3 in vmx_set_host_fs_gs()" KVM: arm64: Don't miss pending interrupts for suspended vCPU
2 parents 5751153 + ece32a7 commit fb184c4

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

arch/arm64/kvm/psci.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
4646
* specification (ARM DEN 0022A). This means all suspend states
4747
* for KVM will preserve the register state.
4848
*/
49-
kvm_vcpu_halt(vcpu);
50-
kvm_clear_request(KVM_REQ_UNHALT, vcpu);
49+
kvm_vcpu_wfi(vcpu);
5150

5251
return PSCI_RET_SUCCESS;
5352
}

arch/x86/kvm/vmx/nested.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
246246
src = &prev->host_state;
247247
dest = &vmx->loaded_vmcs->host_state;
248248

249-
vmx_set_vmcs_host_state(dest, src->cr3, src->fs_sel, src->gs_sel,
250-
src->fs_base, src->gs_base);
249+
vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
251250
dest->ldt_sel = src->ldt_sel;
252251
#ifdef CONFIG_X86_64
253252
dest->ds_sel = src->ds_sel;
@@ -3056,7 +3055,7 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
30563055
static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
30573056
{
30583057
struct vcpu_vmx *vmx = to_vmx(vcpu);
3059-
unsigned long cr4;
3058+
unsigned long cr3, cr4;
30603059
bool vm_fail;
30613060

30623061
if (!nested_early_check)
@@ -3079,6 +3078,12 @@ static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
30793078
*/
30803079
vmcs_writel(GUEST_RFLAGS, 0);
30813080

3081+
cr3 = __get_current_cr3_fast();
3082+
if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3083+
vmcs_writel(HOST_CR3, cr3);
3084+
vmx->loaded_vmcs->host_state.cr3 = cr3;
3085+
}
3086+
30823087
cr4 = cr4_read_shadow();
30833088
if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
30843089
vmcs_writel(HOST_CR4, cr4);

arch/x86/kvm/vmx/vmx.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,14 +1080,9 @@ static void pt_guest_exit(struct vcpu_vmx *vmx)
10801080
wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
10811081
}
10821082

1083-
void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3,
1084-
u16 fs_sel, u16 gs_sel,
1085-
unsigned long fs_base, unsigned long gs_base)
1083+
void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1084+
unsigned long fs_base, unsigned long gs_base)
10861085
{
1087-
if (unlikely(cr3 != host->cr3)) {
1088-
vmcs_writel(HOST_CR3, cr3);
1089-
host->cr3 = cr3;
1090-
}
10911086
if (unlikely(fs_sel != host->fs_sel)) {
10921087
if (!(fs_sel & 7))
10931088
vmcs_write16(HOST_FS_SELECTOR, fs_sel);
@@ -1182,9 +1177,7 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
11821177
gs_base = segment_base(gs_sel);
11831178
#endif
11841179

1185-
vmx_set_vmcs_host_state(host_state, __get_current_cr3_fast(),
1186-
fs_sel, gs_sel, fs_base, gs_base);
1187-
1180+
vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
11881181
vmx->guest_state_loaded = true;
11891182
}
11901183

@@ -6791,7 +6784,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
67916784
static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
67926785
{
67936786
struct vcpu_vmx *vmx = to_vmx(vcpu);
6794-
unsigned long cr4;
6787+
unsigned long cr3, cr4;
67956788

67966789
/* Record the guest's net vcpu time for enforced NMI injections. */
67976790
if (unlikely(!enable_vnmi &&
@@ -6834,6 +6827,19 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
68346827
vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
68356828
vcpu->arch.regs_dirty = 0;
68366829

6830+
/*
6831+
* Refresh vmcs.HOST_CR3 if necessary. This must be done immediately
6832+
* prior to VM-Enter, as the kernel may load a new ASID (PCID) any time
6833+
* it switches back to the current->mm, which can occur in KVM context
6834+
* when switching to a temporary mm to patch kernel code, e.g. if KVM
6835+
* toggles a static key while handling a VM-Exit.
6836+
*/
6837+
cr3 = __get_current_cr3_fast();
6838+
if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6839+
vmcs_writel(HOST_CR3, cr3);
6840+
vmx->loaded_vmcs->host_state.cr3 = cr3;
6841+
}
6842+
68376843
cr4 = cr4_read_shadow();
68386844
if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
68396845
vmcs_writel(HOST_CR4, cr4);

arch/x86/kvm/vmx/vmx.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,8 @@ int allocate_vpid(void);
374374
void free_vpid(int vpid);
375375
void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
376376
void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
377-
void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3,
378-
u16 fs_sel, u16 gs_sel,
379-
unsigned long fs_base, unsigned long gs_base);
377+
void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
378+
unsigned long fs_base, unsigned long gs_base);
380379
int vmx_get_cpl(struct kvm_vcpu *vcpu);
381380
bool vmx_emulation_required(struct kvm_vcpu *vcpu);
382381
unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);

tools/testing/selftests/kvm/aarch64/arch_timer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ static struct kvm_vm *test_vm_create(void)
366366
{
367367
struct kvm_vm *vm;
368368
unsigned int i;
369+
int ret;
369370
int nr_vcpus = test_args.nr_vcpus;
370371

371372
vm = vm_create_default_with_vcpus(nr_vcpus, 0, 0, guest_code, NULL);
@@ -382,7 +383,11 @@ static struct kvm_vm *test_vm_create(void)
382383

383384
ucall_init(vm, NULL);
384385
test_init_timer_irq(vm);
385-
vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
386+
ret = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
387+
if (ret < 0) {
388+
print_skip("Failed to create vgic-v3");
389+
exit(KSFT_SKIP);
390+
}
386391

387392
/* Make all the test's cmdline args visible to the guest */
388393
sync_global_to_guest(vm, test_args);

tools/testing/selftests/kvm/aarch64/vgic_irq.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,10 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
761761

762762
gic_fd = vgic_v3_setup(vm, 1, nr_irqs,
763763
GICD_BASE_GPA, GICR_BASE_GPA);
764+
if (gic_fd < 0) {
765+
print_skip("Failed to create vgic-v3, skipping");
766+
exit(KSFT_SKIP);
767+
}
764768

765769
vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT,
766770
guest_irq_handlers[args.eoi_split][args.level_sensitive]);

tools/testing/selftests/kvm/lib/aarch64/vgic.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs,
5252
nr_vcpus, nr_vcpus_created);
5353

5454
/* Distributor setup */
55-
gic_fd = kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3, false);
55+
if (_kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3,
56+
false, &gic_fd) != 0)
57+
return -1;
5658

5759
kvm_device_access(gic_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
5860
0, &nr_irqs, true);

0 commit comments

Comments
 (0)