hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/kernel/kexec.c
....@@ -110,6 +110,14 @@
110110 unsigned long i;
111111 int ret;
112112
113
+ /*
114
+ * Because we write directly to the reserved memory region when loading
115
+ * crash kernels we need a serialization here to prevent multiple crash
116
+ * kernels from attempting to load simultaneously.
117
+ */
118
+ if (!kexec_trylock())
119
+ return -EBUSY;
120
+
113121 if (flags & KEXEC_ON_CRASH) {
114122 dest_image = &kexec_crash_image;
115123 if (kexec_crash_image)
....@@ -121,7 +129,8 @@
121129 if (nr_segments == 0) {
122130 /* Uninstall image */
123131 kimage_free(xchg(dest_image, NULL));
124
- return 0;
132
+ ret = 0;
133
+ goto out_unlock;
125134 }
126135 if (flags & KEXEC_ON_CRASH) {
127136 /*
....@@ -134,7 +143,7 @@
134143
135144 ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
136145 if (ret)
137
- return ret;
146
+ goto out_unlock;
138147
139148 if (flags & KEXEC_PRESERVE_CONTEXT)
140149 image->preserve_context = 1;
....@@ -171,6 +180,8 @@
171180 arch_kexec_protect_crashkres();
172181
173182 kimage_free(image);
183
+out_unlock:
184
+ kexec_unlock();
174185 return ret;
175186 }
176187
....@@ -247,20 +258,7 @@
247258 ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
248259 return -EINVAL;
249260
250
- /* Because we write directly to the reserved memory
251
- * region when loading crash kernels we need a mutex here to
252
- * prevent multiple crash kernels from attempting to load
253
- * simultaneously, and to prevent a crash kernel from loading
254
- * over the top of a in use crash kernel.
255
- *
256
- * KISS: always take the mutex.
257
- */
258
- if (!mutex_trylock(&kexec_mutex))
259
- return -EBUSY;
260
-
261261 result = do_kexec_load(entry, nr_segments, segments, flags);
262
-
263
- mutex_unlock(&kexec_mutex);
264262
265263 return result;
266264 }
....@@ -301,20 +299,7 @@
301299 return -EFAULT;
302300 }
303301
304
- /* Because we write directly to the reserved memory
305
- * region when loading crash kernels we need a mutex here to
306
- * prevent multiple crash kernels from attempting to load
307
- * simultaneously, and to prevent a crash kernel from loading
308
- * over the top of a in use crash kernel.
309
- *
310
- * KISS: always take the mutex.
311
- */
312
- if (!mutex_trylock(&kexec_mutex))
313
- return -EBUSY;
314
-
315302 result = do_kexec_load(entry, nr_segments, ksegments, flags);
316
-
317
- mutex_unlock(&kexec_mutex);
318303
319304 return result;
320305 }