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);
....@@ -989,14 +989,17 @@
989989 }
990990 }
991991
992
-size_t crash_get_memory_size(void)
992
+ssize_t crash_get_memory_size(void)
993993 {
994
- size_t size = 0;
994
+ ssize_t size = 0;
995995
996
- mutex_lock(&kexec_mutex);
996
+ if (!kexec_trylock())
997
+ return -EBUSY;
998
+
997999 if (crashk_res.end != crashk_res.start)
9981000 size = resource_size(&crashk_res);
999
- mutex_unlock(&kexec_mutex);
1001
+
1002
+ kexec_unlock();
10001003 return size;
10011004 }
10021005
....@@ -1016,7 +1019,8 @@
10161019 unsigned long old_size;
10171020 struct resource *ram_res;
10181021
1019
- mutex_lock(&kexec_mutex);
1022
+ if (!kexec_trylock())
1023
+ return -EBUSY;
10201024
10211025 if (kexec_crash_image) {
10221026 ret = -ENOENT;
....@@ -1025,6 +1029,7 @@
10251029 start = crashk_res.start;
10261030 end = crashk_res.end;
10271031 old_size = (end == 0) ? 0 : end - start + 1;
1032
+ new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
10281033 if (new_size >= old_size) {
10291034 ret = (new_size == old_size) ? 0 : -EINVAL;
10301035 goto unlock;
....@@ -1036,9 +1041,7 @@
10361041 goto unlock;
10371042 }
10381043
1039
- start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1040
- end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
1041
-
1044
+ end = start + new_size;
10421045 crash_free_reserved_phys_range(end, crashk_res.end);
10431046
10441047 if ((start == end) && (crashk_res.parent != NULL))
....@@ -1054,7 +1057,7 @@
10541057 insert_resource(&iomem_resource, ram_res);
10551058
10561059 unlock:
1057
- mutex_unlock(&kexec_mutex);
1060
+ kexec_unlock();
10581061 return ret;
10591062 }
10601063
....@@ -1126,7 +1129,7 @@
11261129 {
11271130 int error = 0;
11281131
1129
- if (!mutex_trylock(&kexec_mutex))
1132
+ if (!kexec_trylock())
11301133 return -EBUSY;
11311134 if (!kexec_image) {
11321135 error = -EINVAL;
....@@ -1201,7 +1204,7 @@
12011204 #endif
12021205
12031206 Unlock:
1204
- mutex_unlock(&kexec_mutex);
1207
+ kexec_unlock();
12051208 return error;
12061209 }
12071210