@@ -110,6 +110,17 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
110
110
unsigned long i ;
111
111
int ret ;
112
112
113
+ /*
114
+ * Because we write directly to the reserved memory region when loading
115
+ * crash kernels we need a mutex here to prevent multiple crash kernels
116
+ * from attempting to load simultaneously, and to prevent a crash kernel
117
+ * from loading over the top of a in use crash kernel.
118
+ *
119
+ * KISS: always take the mutex.
120
+ */
121
+ if (!mutex_trylock (& kexec_mutex ))
122
+ return - EBUSY ;
123
+
113
124
if (flags & KEXEC_ON_CRASH ) {
114
125
dest_image = & kexec_crash_image ;
115
126
if (kexec_crash_image )
@@ -121,7 +132,8 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
121
132
if (nr_segments == 0 ) {
122
133
/* Uninstall image */
123
134
kimage_free (xchg (dest_image , NULL ));
124
- return 0 ;
135
+ ret = 0 ;
136
+ goto out_unlock ;
125
137
}
126
138
if (flags & KEXEC_ON_CRASH ) {
127
139
/*
@@ -134,7 +146,7 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
134
146
135
147
ret = kimage_alloc_init (& image , entry , nr_segments , segments , flags );
136
148
if (ret )
137
- return ret ;
149
+ goto out_unlock ;
138
150
139
151
if (flags & KEXEC_PRESERVE_CONTEXT )
140
152
image -> preserve_context = 1 ;
@@ -171,6 +183,8 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
171
183
arch_kexec_protect_crashkres ();
172
184
173
185
kimage_free (image );
186
+ out_unlock :
187
+ mutex_unlock (& kexec_mutex );
174
188
return ret ;
175
189
}
176
190
@@ -247,21 +261,8 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
247
261
((flags & KEXEC_ARCH_MASK ) != KEXEC_ARCH_DEFAULT ))
248
262
return - EINVAL ;
249
263
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
-
261
264
result = do_kexec_load (entry , nr_segments , segments , flags );
262
265
263
- mutex_unlock (& kexec_mutex );
264
-
265
266
return result ;
266
267
}
267
268
@@ -301,21 +302,8 @@ COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
301
302
return - EFAULT ;
302
303
}
303
304
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
-
315
305
result = do_kexec_load (entry , nr_segments , ksegments , flags );
316
306
317
- mutex_unlock (& kexec_mutex );
318
-
319
307
return result ;
320
308
}
321
309
#endif
0 commit comments