On Tue, Oct 04, 2022, Vitaly Kuznetsov wrote: > @@ -2225,10 +2264,27 @@ static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) > > static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) > { > + int ret; > + > trace_kvm_hv_hypercall_done(result); > kvm_hv_hypercall_set_result(vcpu, result); > ++vcpu->stat.hypercalls; > - return kvm_skip_emulated_instruction(vcpu); > + ret = kvm_skip_emulated_instruction(vcpu); > + > + if (unlikely(hv_result_success(result) && is_guest_mode(vcpu) > + && kvm_hv_is_tlb_flush_hcall(vcpu))) { "&&" goes on the previous line. > + struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); > + u32 tlb_lock_count; > + > + if (unlikely(kvm_read_guest(vcpu->kvm, hv_vcpu->nested.pa_page_gpa, Nit, I'd say leave off the "unlikely", the hints almost never provide meaningful performance benefits, e.g. code generation is identical with and without the unlikely, and IMO the extra line length and parantheses depth makes the code harder to read. > + &tlb_lock_count, sizeof(tlb_lock_count)))) > + kvm_inject_gp(vcpu, 0); This will inject a #GP on the _next_ instruction. That seems wrong. And why #GP in the first place? E.g. if userspace yanks out the memslot, injecting #GP into the guest is less-than-ideal behavior. What about reading tlb_lock_count before skipping the hypercall, e.g. static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) { u32 tlb_lock_count = 0; int ret; if (hv_result_success(result) && is_guest_mode(vcpu) && kvm_hv_is_tlb_flush_hcall(vcpu) && kvm_read_guest(vcpu->kvm, to_hv_vcpu(vcpu)->nested.pa_page_gpa, &tlb_lock_count, sizeof(tlb_lock_count))) result = HV_STATUS_INVALID_HYPERCALL_INPUT; trace_kvm_hv_hypercall_done(result); kvm_hv_hypercall_set_result(vcpu, result); ++vcpu->stat.hypercalls; ret = kvm_skip_emulated_instruction(vcpu); if (tlb_lock_count) kvm_x86_ops.nested_ops->hv_inject_synthetic_vmexit_post_tlb_flush(vcpu); return ret; } > + > + if (tlb_lock_count) tlb_lock_count will be uninitialized if kvm_read_guest() fails. > + kvm_x86_ops.nested_ops->hv_inject_synthetic_vmexit_post_tlb_flush(vcpu); Ugh, kvm_skip_emulated_instruction() is flawed. If skipping the emulated instruction fails, i.e. if EMULTYPE_SKIP emulation fails, then synthesizing a VM-Exit is technically wrong. But kvm_skip_emulated_instruction() also returns '0' for a KVM_EXIT_DEBUG, which happens after skipping the instruction. Not worth handling here, e.g. nested_svm_vmrun() has the same "bug". Failure is effectively limited to old AMD CPUs, and userspace is likely goiing to kill the VM anyways.