KVM: s390: fix migration mode teardown in memslot handling
Subsystems (4)
cover KVM: s390: fix migration mode teardown in memslot handling
Two small fixes for migration mode handling ins390_kvm_mmu_prepare_memory_region().
Patch 1 fixes an error-path bug: if the mmu cache reservation fails after migration mode was already stopped, the ioctl fails but the CMMA state cleared by kvm_s390_vm_stop_migration() cannot be restored, so an in-flight migration aborts over an unrelated allocation failure. Reserving the cache first keeps both memslots and migration state untouched on failure.
Patch 2 is a cleanup on top: kvm_s390_vm_stop_migration() cannot fail, so it returns void and the always-false WARN() at its call site is dropped.
Gautam Gala (2):
KVM: s390: reserve mmu cache before stopping migration mode
KVM: s390: make kvm_s390_vm_stop_migration() void
arch/s390/kvm/arm64/arm.h | 3 +-- arch/s390/kvm/gmap/kvm_mmu.c | 18 ++++++++++-------- arch/s390/kvm/s390/s390.c | 10 +++++----- arch/s390/kvm/s390/s390.h | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/2 KVM: s390: reserve mmu cache before stopping migration mode
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.
--- arch/s390/kvm/gmap/kvm_mmu.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/s390/kvm/gmap/kvm_mmu.c +9 -6
@@ -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 )
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v1 1/2] KVM: s390: reserve mmu cache before stopping migration mode
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines
Creating or moving a memslot without
KVM_MEM_LOG_DIRTY_PAGESstops migration mode ins390_kvm_mmu_prepare_memory_region(). If the mmu cache reservation fails afterwards, the ioctl returns-ENOMEMwith the memslots unchanged, but migration mode stays stopped: the CMMA state cleared bykvm_s390_vm_stop_migration()cannot be restored, so an in-flight migration aborts over an unrelated allocation failure.
Quoted 6 lines
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 <[email protected]>Signed-off-by Gautam Gala <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/2 KVM: s390: make kvm_s390_vm_stop_migration() void
In reply to Gautam Galakvm_s390_vm_stop_migration() returns 0 on all paths; it cannot fail. Its caller in s390_kvm_mmu_prepare_memory_region() wraps it in WARN(), producing a condition that is always false. The arm64 stub returns -EINVAL but is unreachable because kvm_s390_is_migration_mode() returns false there.
Change the return type to void and drop the dead WARN(). The KVM_S390_VM_MIGRATION_STOP attribute handler sets its result to 0 explicitly; userspace behavior is unchanged since stopping migration never failed.
--- arch/s390/kvm/arm64/arm.h | 3 +-- arch/s390/kvm/gmap/kvm_mmu.c | 3 +-- arch/s390/kvm/s390/s390.c | 10 +++++----- arch/s390/kvm/s390/s390.h | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-)
arch/s390/kvm/arm64/arm.h +1 -2
@@ -50,9 +50,8 @@ static __always_inline void kvm_s390_update_cmma_dirty(struct kvm *kvm, const st }/* should never be called */ -static __always_inline int kvm_s390_vm_stop_migration(struct kvm *kvm) +static __always_inline void kvm_s390_vm_stop_migration(struct kvm *kvm) { -return -EINVAL ; }#endif /* ARCH_S390_KVM_ARM64_H */
arch/s390/kvm/gmap/kvm_mmu.c +1 -2
@@ -99,8 +99,7 @@ int s390_kvm_mmu_prepare_memory_region(struct kvm *kvm,*/ if (change !=KVM_MR_DELETE && !(new->flags &KVM_MEM_LOG_DIRTY_PAGES )) -WARN (kvm_s390_vm_stop_migration (kvm), -"Failed to stop migration mode" ); +kvm_s390_vm_stop_migration (kvm); }if (change ==KVM_MR_FLAGS_ONLY )
arch/s390/kvm/s390/s390.c +5 -5
@@ -1242,11 +1242,11 @@ static int kvm_s390_vm_start_migration(struct kvm *kvm)* Must be called with kvm->slots_arch_lock to avoid races with ourselves, * kvm_s390_vm_start_migration() and kvm_s390_get_cmma_bits(). */ -int kvm_s390_vm_stop_migration (struct kvm *kvm) +void kvm_s390_vm_stop_migration (struct kvm *kvm) {/* migration mode already disabled */ if (!kvm->arch.migration_mode) -return 0 ; +return ;/* * Unset the flag and propagate to all vCPUs. From now on the cmma_d * bit will not be touched on any PGSTE.
@@ -1255,7 +1255,7 @@ int kvm_s390_vm_stop_migration(struct kvm *kvm)*/ WRITE_ONCE (kvm->arch.migration_mode,0 );if (!kvm->arch.use_cmma) -return 0 ; +return ;kvm_s390_sync_request_broadcast (kvm,KVM_REQ_STOP_MIGRATION );/* Clear cmma_d on all existing PGSTEs and set cmma_dirty_pages to 0. */
@@ -1266,7 +1266,6 @@ int kvm_s390_vm_stop_migration(struct kvm *kvm)* cmma_dirty_pages is 0, and all existing PGSTEs have their cmma_d bit * set to 0. */ -return 0 ; } static int kvm_s390_vm_set_migration(struct kvm *kvm,
@@ -1282,7 +1281,8 @@ static int kvm_s390_vm_set_migration(struct kvm *kvm, res =kvm_s390_vm_start_migration (kvm);break ;case KVM_S390_VM_MIGRATION_STOP : - res =kvm_s390_vm_stop_migration (kvm); +kvm_s390_vm_stop_migration (kvm); + res =0 ;break ;default :break ;
arch/s390/kvm/s390/s390.h +1 -1
@@ -474,7 +474,7 @@ int __kvm_s390_mprotect_many(struct gmap *gmap, gpa_t gpa, u8 npages, unsigned ibool kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu);void kvm_s390_update_cmma_dirty (struct kvm *kvm,const struct kvm_memory_slot *old); -int kvm_s390_vm_stop_migration (struct kvm *kvm); +void kvm_s390_vm_stop_migration (struct kvm *kvm);/* implemented in diag.c */
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v1 2/2] KVM: s390: make kvm_s390_vm_stop_migration() void
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
kvm_s390_vm_stop_migration()returns 0 on all paths; it cannot fail. Its caller ins390_kvm_mmu_prepare_memory_region()wraps it inWARN(), producing a condition that is always false. The arm64 stub returns-EINVALbut is unreachable becausekvm_s390_is_migration_mode()returns false there.
Quoted 6 lines
Change the return type to void and drop the dead
WARN(). TheKVM_S390_VM_MIGRATION_STOPattribute handler sets its result to 0 explicitly; userspace behavior is unchanged since stopping migration never failed.Signed-off-by Gautam Gala <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v1 2/2] KVM: s390: make kvm_s390_vm_stop_migration() void
In reply to Gautam GalaQuoted 25 lines · Gautam Gala
kvm_s390_vm_stop_migration()returns 0 on all paths; it cannot fail. Its caller ins390_kvm_mmu_prepare_memory_region()wraps it inWARN(), producing a condition that is always false. The arm64 stub returns-EINVALbut is unreachable becausekvm_s390_is_migration_mode()returns false there.Change the return type to void and drop the dead
WARN(). TheKVM_S390_VM_MIGRATION_STOPattribute handler sets its result to 0 explicitly; userspace behavior is unchanged since stopping migration never failed.Signed-off-by Gautam Gala <[email protected]>Reviewed-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/arm64/arm.h | 3 +-- arch/s390/kvm/gmap/kvm_mmu.c | 3 +-- arch/s390/kvm/s390/s390.c | 10 +++++----- arch/s390/kvm/s390/s390.h | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-)arch/s390/kvm/arm64/arm.h +0
-0
arch/s390/kvm/arm64/arm.h static __always_inline void kvm_s390_update_cmma_dirty(struct kvm *kvm, const st · line 55
Quoted 7 lines
arch/s390/kvm/arm64/arm.h +1
-2@@ -50,9 +50,8 @@ static __always_inline void kvm_s390_update_cmma_dirty(struct kvm *kvm, const st }/* should never be called */ -static __always_inline int kvm_s390_vm_stop_migration(struct kvm *kvm) +static __always_inline void kvm_s390_vm_stop_migration(struct kvm *kvm) { -return -EINVAL ;
should we maybe add something like BUILD_BUG_ON(1) here, so it doesn't fail silently when the underlying assumption (will never be called because kvm_s390_is_migration_mode() always return false) stops holding for some reason?
BUILD_BUG_ON(1) won't fire if the compiler can prove the path is never taken.
Beyond that, this change makes sense to me on the arm64 side.
Am 20.11.26 um 08:53 schrieb Gautam Gala:
Quoted 18 lines
Two small fixes for migration mode handling in
s390_kvm_mmu_prepare_memory_region().Patch 1 fixes an error-path bug: if the mmu cache reservation fails after migration mode was already stopped, the ioctl fails but the CMMA state cleared by
kvm_s390_vm_stop_migration()cannot be restored, so an in-flight migration aborts over an unrelated allocation failure. Reserving the cache first keeps both memslots and migration state untouched on failure.Patch 2 is a cleanup on top:
kvm_s390_vm_stop_migration()cannot fail, so it returns void and the always-falseWARN()at its call site is dropped.Gautam Gala (2):
KVM: s390: reserve mmu cache before stopping migration mode
KVM: s390: makekvm_s390_vm_stop_migration()void
Quoted 5 lines
arch/s390/kvm/arm64/arm.h | 3 +-- arch/s390/kvm/gmap/kvm_mmu.c | 18 ++++++++++-------- arch/s390/kvm/s390/s390.c | 10 +++++----- arch/s390/kvm/s390/s390.h | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-)
Thanks applied.