hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
kernel/kernel/kexec_core.c
....@@ -45,7 +45,7 @@
4545 #include <crypto/sha.h>
4646 #include "kexec_internal.h"
4747
48
-DEFINE_MUTEX(kexec_mutex);
48
+atomic_t __kexec_lock = ATOMIC_INIT(0);
4949
5050 /* Per cpu memory for storing cpu states in case of system crash. */
5151 note_buf_t __percpu *crash_notes;
....@@ -943,7 +943,7 @@
943943 */
944944 void __noclone __crash_kexec(struct pt_regs *regs)
945945 {
946
- /* Take the kexec_mutex here to prevent sys_kexec_load
946
+ /* Take the kexec_lock here to prevent sys_kexec_load
947947 * running on one cpu from replacing the crash kernel
948948 * we are using after a panic on a different cpu.
949949 *
....@@ -951,7 +951,7 @@
951951 * of memory the xchg(&kexec_crash_image) would be
952952 * sufficient. But since I reuse the memory...
953953 */
954
- if (mutex_trylock(&kexec_mutex)) {
954
+ if (kexec_trylock()) {
955955 if (kexec_crash_image) {
956956 struct pt_regs fixed_regs;
957957
....@@ -960,7 +960,7 @@
960960 machine_crash_shutdown(&fixed_regs);
961961 machine_kexec(kexec_crash_image);
962962 }
963
- mutex_unlock(&kexec_mutex);
963
+ kexec_unlock();
964964 }
965965 }
966966 STACK_FRAME_NON_STANDARD(__crash_kexec);
....@@ -978,6 +978,7 @@
978978 old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
979979 if (old_cpu == PANIC_CPU_INVALID) {
980980 /* This is the 1st CPU which comes here, so go ahead. */
981
+ printk_safe_flush_on_panic();
981982 __crash_kexec(regs);
982983
983984 /*
....@@ -988,14 +989,17 @@
988989 }
989990 }
990991
991
-size_t crash_get_memory_size(void)
992
+ssize_t crash_get_memory_size(void)
992993 {
993
- size_t size = 0;
994
+ ssize_t size = 0;
994995
995
- mutex_lock(&kexec_mutex);
996
+ if (!kexec_trylock())
997
+ return -EBUSY;
998
+
996999 if (crashk_res.end != crashk_res.start)
9971000 size = resource_size(&crashk_res);
998
- mutex_unlock(&kexec_mutex);
1001
+
1002
+ kexec_unlock();
9991003 return size;
10001004 }
10011005
....@@ -1015,7 +1019,8 @@
10151019 unsigned long old_size;
10161020 struct resource *ram_res;
10171021
1018
- mutex_lock(&kexec_mutex);
1022
+ if (!kexec_trylock())
1023
+ return -EBUSY;
10191024
10201025 if (kexec_crash_image) {
10211026 ret = -ENOENT;
....@@ -1024,6 +1029,7 @@
10241029 start = crashk_res.start;
10251030 end = crashk_res.end;
10261031 old_size = (end == 0) ? 0 : end - start + 1;
1032
+ new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
10271033 if (new_size >= old_size) {
10281034 ret = (new_size == old_size) ? 0 : -EINVAL;
10291035 goto unlock;
....@@ -1035,9 +1041,7 @@
10351041 goto unlock;
10361042 }
10371043
1038
- start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1039
- end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
1040
-
1044
+ end = start + new_size;
10411045 crash_free_reserved_phys_range(end, crashk_res.end);
10421046
10431047 if ((start == end) && (crashk_res.parent != NULL))
....@@ -1053,7 +1057,7 @@
10531057 insert_resource(&iomem_resource, ram_res);
10541058
10551059 unlock:
1056
- mutex_unlock(&kexec_mutex);
1060
+ kexec_unlock();
10571061 return ret;
10581062 }
10591063
....@@ -1125,7 +1129,7 @@
11251129 {
11261130 int error = 0;
11271131
1128
- if (!mutex_trylock(&kexec_mutex))
1132
+ if (!kexec_trylock())
11291133 return -EBUSY;
11301134 if (!kexec_image) {
11311135 error = -EINVAL;
....@@ -1200,7 +1204,7 @@
12001204 #endif
12011205
12021206 Unlock:
1203
- mutex_unlock(&kexec_mutex);
1207
+ kexec_unlock();
12041208 return error;
12051209 }
12061210