Implement KVM_CAP_MEMORY_FAULT_INFO for uaccess failures in kvm_vcpu_write_guest_page() Signed-off-by: Anish Moorthy <amoorthy@xxxxxxxxxx> --- virt/kvm/kvm_main.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d9c0fa7c907f..ea27a8178f1a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3090,8 +3090,10 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); /* * Copy 'len' bytes from 'data' into guest memory at '(gfn * PAGE_SIZE) + offset' + * If 'vcpu' is non-null, then may fill its run struct for a + * KVM_EXIT_MEMORY_FAULT on uaccess failure. */ -static int __kvm_write_guest_page(struct kvm *kvm, +static int __kvm_write_guest_page(struct kvm *kvm, struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, gfn_t gfn, const void *data, int offset, int len) { @@ -3102,8 +3104,13 @@ static int __kvm_write_guest_page(struct kvm *kvm, if (kvm_is_error_hva(addr)) return -EFAULT; r = __copy_to_user((void __user *)addr + offset, data, len); - if (r) + if (r) { + if (vcpu) + kvm_populate_efault_info(vcpu, gfn * PAGE_SIZE + offset, + len, + KVM_MEMORY_FAULT_FLAG_WRITE); return -EFAULT; + } mark_page_dirty_in_slot(kvm, memslot, gfn); return 0; } @@ -3113,7 +3120,7 @@ int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); - return __kvm_write_guest_page(kvm, slot, gfn, data, offset, len); + return __kvm_write_guest_page(kvm, NULL, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_write_guest_page); @@ -3121,8 +3128,8 @@ int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data, int offset, int len) { struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); - - return __kvm_write_guest_page(vcpu->kvm, slot, gfn, data, offset, len); + return __kvm_write_guest_page(vcpu->kvm, vcpu, slot, gfn, data, + offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page); -- 2.41.0.rc0.172.g3f132b7071-goog