Creating or moving a memslot without KVM_MEM_LOG_DIRTY_PAGES stops migration mode in s390_kvm_mmu_prepare_memory_region(). If the mmu cache reservation fails afterwards, the ioctl returns -ENOMEM with the memslots unchanged, but migration mode stays stopped: the CMMA state cleared by kvm_s390_vm_stop_migration() cannot be restored, so an in-flight migration aborts over an unrelated allocation failure. Reserve the mmu cache before tearing down migration mode so the error path leaves both memslots and migration state untouched. Fixes: 19192a404327 ("KVM: s390: Move all code into s390_kvm_mmu_prepare_memory_region()") Reported-by: Christian Borntraeger Signed-off-by: Gautam Gala Reviewed-by: Christian Borntraeger Reviewed-by: Claudio Imbrenda --- arch/s390/kvm/gmap/kvm_mmu.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/arch/s390/kvm/gmap/kvm_mmu.c b/arch/s390/kvm/gmap/kvm_mmu.c index 4e2da60b9859..120980cf6ac6 100644 --- a/arch/s390/kvm/gmap/kvm_mmu.c +++ b/arch/s390/kvm/gmap/kvm_mmu.c @@ -78,6 +78,14 @@ int s390_kvm_mmu_prepare_memory_region(struct kvm *kvm, return -EINVAL; if (!asce_contains_gfn(kvm->arch.gmap->asce, new->base_gfn + new->npages - 1)) return -EINVAL; + + /* + * Must be allocated before migration mode is stopped below: + * kvm_s390_vm_stop_migration() is not rolled back on failure. + */ + mc = kvm_s390_new_mmu_cache(); + if (!mc) + return -ENOMEM; } if (kvm_s390_is_migration_mode(kvm)) { @@ -97,12 +105,7 @@ int s390_kvm_mmu_prepare_memory_region(struct kvm *kvm, if (change == KVM_MR_FLAGS_ONLY) return 0; - if (change != KVM_MR_DELETE) { - /* Enough capacity to add a new memslot */ - mc = kvm_s390_new_mmu_cache(); - if (!mc) - return -ENOMEM; - } + scoped_guard(write_lock, &kvm->mmu_lock) { kvm_s390_update_cmma_dirty(kvm, old); if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) -- 2.56.0