Skip to content

Commit 4b692e8

Browse files
arndbtorvalds
authored andcommitted
kexec: move locking into do_kexec_load
Patch series "compat: remove compat_alloc_user_space", v5. Going through compat_alloc_user_space() to convert indirect system call arguments tends to add complexity compared to handling the native and compat logic in the same code. This patch (of 6): The locking is the same between the native and compat version of sys_kexec_load(), so it can be done in the common implementation to reduce duplication. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Arnd Bergmann <[email protected]> Co-developed-by: Eric Biederman <[email protected]> Co-developed-by: Christoph Hellwig <[email protected]> Acked-by: "Eric W. Biederman" <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Will Deacon <[email protected]> Cc: Thomas Bogendoerfer <[email protected]> Cc: "James E.J. Bottomley" <[email protected]> Cc: Helge Deller <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: Vasily Gorbik <[email protected]> Cc: Christian Borntraeger <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Al Viro <[email protected]> Cc: Feng Tang <[email protected]> Cc: Christoph Hellwig <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 213ecb3 commit 4b692e8

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

kernel/kexec.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
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 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+
113124
if (flags & KEXEC_ON_CRASH) {
114125
dest_image = &kexec_crash_image;
115126
if (kexec_crash_image)
@@ -121,7 +132,8 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
121132
if (nr_segments == 0) {
122133
/* Uninstall image */
123134
kimage_free(xchg(dest_image, NULL));
124-
return 0;
135+
ret = 0;
136+
goto out_unlock;
125137
}
126138
if (flags & KEXEC_ON_CRASH) {
127139
/*
@@ -134,7 +146,7 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
134146

135147
ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
136148
if (ret)
137-
return ret;
149+
goto out_unlock;
138150

139151
if (flags & KEXEC_PRESERVE_CONTEXT)
140152
image->preserve_context = 1;
@@ -171,6 +183,8 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
171183
arch_kexec_protect_crashkres();
172184

173185
kimage_free(image);
186+
out_unlock:
187+
mutex_unlock(&kexec_mutex);
174188
return ret;
175189
}
176190

@@ -247,21 +261,8 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
247261
((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
248262
return -EINVAL;
249263

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-
261264
result = do_kexec_load(entry, nr_segments, segments, flags);
262265

263-
mutex_unlock(&kexec_mutex);
264-
265266
return result;
266267
}
267268

@@ -301,21 +302,8 @@ COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
301302
return -EFAULT;
302303
}
303304

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-
315305
result = do_kexec_load(entry, nr_segments, ksegments, flags);
316306

317-
mutex_unlock(&kexec_mutex);
318-
319307
return result;
320308
}
321309
#endif

0 commit comments

Comments
 (0)