]> git.hungrycats.org Git - linux/commitdiff
KVM: Use goto to handle errors during memslot preparation
authorSean Christopherson <seanjc@google.com>
Tue, 22 Sep 2026 00:13:28 +0000 (17:13 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:09 +0000 (12:06 -0500)
commit 23a205797cec86f391d0be886b9bc741e1262063 upstream.

Use a goto to unwind early memslot changes if preparing for a memslot
operation fails.  This will allow moving the creation of guest_memfd
bindings into kvm_set_memslot() without needing to copy+paste the unwind
logic.

No functional change intended.

Cc: stable@vger.kernel.org
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Ackerley Tng <ackerleytng@google.com>
Link: https://patch.msgid.link/20260922001332.1121266-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
virt/kvm/kvm_main.c

index ba09b1dabc4b7e0e6b495d0ab7dbad9ba3827eaa..23e7d1bb80b04dde7072240d23f435905d290a8c 100644 (file)
@@ -1937,21 +1937,8 @@ static int kvm_set_memslot(struct kvm *kvm,
        }
 
        r = kvm_prepare_memory_region(kvm, old, new, change);
-       if (r) {
-               /*
-                * For DELETE/MOVE, revert the above INVALID change.  No
-                * modifications required since the original slot was preserved
-                * in the inactive slots.  Changing the active memslots also
-                * release slots_arch_lock.
-                */
-               if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
-                       kvm_activate_memslot(kvm, invalid_slot, old);
-                       kfree(invalid_slot);
-               } else {
-                       mutex_unlock(&kvm->slots_arch_lock);
-               }
-               return r;
-       }
+       if (r)
+               goto err;
 
        /*
         * For DELETE and MOVE, the working slot is now active as the INVALID
@@ -1983,6 +1970,20 @@ static int kvm_set_memslot(struct kvm *kvm,
        kvm_commit_memory_region(kvm, old, new, change);
 
        return 0;
+
+err:
+       /*
+        * For DELETE/MOVE, revert the above INVALID change.  No modifications
+        * required since the original slot was preserved in the inactive slots.
+        * Changing the active memslots also release slots_arch_lock.
+        */
+       if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
+               kvm_activate_memslot(kvm, invalid_slot, old);
+               kfree(invalid_slot);
+       } else {
+               mutex_unlock(&kvm->slots_arch_lock);
+       }
+       return r;
 }
 
 static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,