unsigned long i;
        int ret;
 
+       /*
+        * Because we write directly to the reserved memory region when loading
+        * crash kernels we need a mutex here to prevent multiple crash kernels
+        * from attempting to load simultaneously, and to prevent a crash kernel
+        * from loading over the top of a in use crash kernel.
+        *
+        * KISS: always take the mutex.
+        */
+       if (!mutex_trylock(&kexec_mutex))
+               return -EBUSY;
+
        if (flags & KEXEC_ON_CRASH) {
                dest_image = &kexec_crash_image;
                if (kexec_crash_image)
        if (nr_segments == 0) {
                /* Uninstall image */
                kimage_free(xchg(dest_image, NULL));
-               return 0;
+               ret = 0;
+               goto out_unlock;
        }
        if (flags & KEXEC_ON_CRASH) {
                /*
 
        ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
        if (ret)
-               return ret;
+               goto out_unlock;
 
        if (flags & KEXEC_PRESERVE_CONTEXT)
                image->preserve_context = 1;
                arch_kexec_protect_crashkres();
 
        kimage_free(image);
+out_unlock:
+       mutex_unlock(&kexec_mutex);
        return ret;
 }
 
                ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
                return -EINVAL;
 
-       /* Because we write directly to the reserved memory
-        * region when loading crash kernels we need a mutex here to
-        * prevent multiple crash  kernels from attempting to load
-        * simultaneously, and to prevent a crash kernel from loading
-        * over the top of a in use crash kernel.
-        *
-        * KISS: always take the mutex.
-        */
-       if (!mutex_trylock(&kexec_mutex))
-               return -EBUSY;
-
        result = do_kexec_load(entry, nr_segments, segments, flags);
 
-       mutex_unlock(&kexec_mutex);
-
        return result;
 }
 
                        return -EFAULT;
        }
 
-       /* Because we write directly to the reserved memory
-        * region when loading crash kernels we need a mutex here to
-        * prevent multiple crash  kernels from attempting to load
-        * simultaneously, and to prevent a crash kernel from loading
-        * over the top of a in use crash kernel.
-        *
-        * KISS: always take the mutex.
-        */
-       if (!mutex_trylock(&kexec_mutex))
-               return -EBUSY;
-
        result = do_kexec_load(entry, nr_segments, ksegments, flags);
 
-       mutex_unlock(&kexec_mutex);
-
        return result;
 }
 #endif