hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/kernel/kexec_core.c
....@@ -1,9 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * kexec.c - kexec system call core code.
34 * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
4
- *
5
- * This source code is licensed under the GNU General Public License,
6
- * Version 2. See the file COPYING for more details.
75 */
86
97 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -38,7 +36,7 @@
3836 #include <linux/syscore_ops.h>
3937 #include <linux/compiler.h>
4038 #include <linux/hugetlb.h>
41
-#include <linux/frame.h>
39
+#include <linux/objtool.h>
4240
4341 #include <asm/page.h>
4442 #include <asm/sections.h>
....@@ -47,7 +45,7 @@
4745 #include <crypto/sha.h>
4846 #include "kexec_internal.h"
4947
50
-DEFINE_MUTEX(kexec_mutex);
48
+atomic_t __kexec_lock = ATOMIC_INIT(0);
5149
5250 /* Per cpu memory for storing cpu states in case of system crash. */
5351 note_buf_t __percpu *crash_notes;
....@@ -111,7 +109,7 @@
111109 * defined more restrictively in <asm/kexec.h>.
112110 *
113111 * The code for the transition from the current kernel to the
114
- * the new kernel is placed in the control_code_buffer, whose size
112
+ * new kernel is placed in the control_code_buffer, whose size
115113 * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
116114 * page of memory is necessary, but some architectures require more.
117115 * Because this memory must be identity mapped in the transition from
....@@ -152,6 +150,7 @@
152150 int i;
153151 unsigned long nr_segments = image->nr_segments;
154152 unsigned long total_pages = 0;
153
+ unsigned long nr_pages = totalram_pages();
155154
156155 /*
157156 * Verify we have good destination addresses. The caller is
....@@ -217,13 +216,13 @@
217216 * wasted allocating pages, which can cause a soft lockup.
218217 */
219218 for (i = 0; i < nr_segments; i++) {
220
- if (PAGE_COUNT(image->segment[i].memsz) > totalram_pages / 2)
219
+ if (PAGE_COUNT(image->segment[i].memsz) > nr_pages / 2)
221220 return -EINVAL;
222221
223222 total_pages += PAGE_COUNT(image->segment[i].memsz);
224223 }
225224
226
- if (total_pages > totalram_pages / 2)
225
+ if (total_pages > nr_pages / 2)
227226 return -EINVAL;
228227
229228 /*
....@@ -590,6 +589,12 @@
590589 kimage_free_page_list(&image->unusable_pages);
591590
592591 }
592
+
593
+int __weak machine_kexec_post_load(struct kimage *image)
594
+{
595
+ return 0;
596
+}
597
+
593598 void kimage_terminate(struct kimage *image)
594599 {
595600 if (*image->entry != 0)
....@@ -938,7 +943,7 @@
938943 */
939944 void __noclone __crash_kexec(struct pt_regs *regs)
940945 {
941
- /* Take the kexec_mutex here to prevent sys_kexec_load
946
+ /* Take the kexec_lock here to prevent sys_kexec_load
942947 * running on one cpu from replacing the crash kernel
943948 * we are using after a panic on a different cpu.
944949 *
....@@ -946,7 +951,7 @@
946951 * of memory the xchg(&kexec_crash_image) would be
947952 * sufficient. But since I reuse the memory...
948953 */
949
- if (mutex_trylock(&kexec_mutex)) {
954
+ if (kexec_trylock()) {
950955 if (kexec_crash_image) {
951956 struct pt_regs fixed_regs;
952957
....@@ -955,7 +960,7 @@
955960 machine_crash_shutdown(&fixed_regs);
956961 machine_kexec(kexec_crash_image);
957962 }
958
- mutex_unlock(&kexec_mutex);
963
+ kexec_unlock();
959964 }
960965 }
961966 STACK_FRAME_NON_STANDARD(__crash_kexec);
....@@ -984,14 +989,17 @@
984989 }
985990 }
986991
987
-size_t crash_get_memory_size(void)
992
+ssize_t crash_get_memory_size(void)
988993 {
989
- size_t size = 0;
994
+ ssize_t size = 0;
990995
991
- mutex_lock(&kexec_mutex);
996
+ if (!kexec_trylock())
997
+ return -EBUSY;
998
+
992999 if (crashk_res.end != crashk_res.start)
9931000 size = resource_size(&crashk_res);
994
- mutex_unlock(&kexec_mutex);
1001
+
1002
+ kexec_unlock();
9951003 return size;
9961004 }
9971005
....@@ -1011,7 +1019,8 @@
10111019 unsigned long old_size;
10121020 struct resource *ram_res;
10131021
1014
- mutex_lock(&kexec_mutex);
1022
+ if (!kexec_trylock())
1023
+ return -EBUSY;
10151024
10161025 if (kexec_crash_image) {
10171026 ret = -ENOENT;
....@@ -1020,6 +1029,7 @@
10201029 start = crashk_res.start;
10211030 end = crashk_res.end;
10221031 old_size = (end == 0) ? 0 : end - start + 1;
1032
+ new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
10231033 if (new_size >= old_size) {
10241034 ret = (new_size == old_size) ? 0 : -EINVAL;
10251035 goto unlock;
....@@ -1031,9 +1041,7 @@
10311041 goto unlock;
10321042 }
10331043
1034
- start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
1035
- end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
1036
-
1044
+ end = start + new_size;
10371045 crash_free_reserved_phys_range(end, crashk_res.end);
10381046
10391047 if ((start == end) && (crashk_res.parent != NULL))
....@@ -1049,7 +1057,7 @@
10491057 insert_resource(&iomem_resource, ram_res);
10501058
10511059 unlock:
1052
- mutex_unlock(&kexec_mutex);
1060
+ kexec_unlock();
10531061 return ret;
10541062 }
10551063
....@@ -1121,7 +1129,7 @@
11211129 {
11221130 int error = 0;
11231131
1124
- if (!mutex_trylock(&kexec_mutex))
1132
+ if (!kexec_trylock())
11251133 return -EBUSY;
11261134 if (!kexec_image) {
11271135 error = -EINVAL;
....@@ -1150,7 +1158,7 @@
11501158 error = dpm_suspend_end(PMSG_FREEZE);
11511159 if (error)
11521160 goto Resume_devices;
1153
- error = disable_nonboot_cpus();
1161
+ error = suspend_disable_secondary_cpus();
11541162 if (error)
11551163 goto Enable_cpus;
11561164 local_irq_disable();
....@@ -1171,7 +1179,7 @@
11711179 * CPU hotplug again; so re-enable it here.
11721180 */
11731181 cpu_hotplug_enable();
1174
- pr_emerg("Starting new kernel\n");
1182
+ pr_notice("Starting new kernel\n");
11751183 machine_shutdown();
11761184 }
11771185
....@@ -1183,7 +1191,7 @@
11831191 Enable_irqs:
11841192 local_irq_enable();
11851193 Enable_cpus:
1186
- enable_nonboot_cpus();
1194
+ suspend_enable_secondary_cpus();
11871195 dpm_resume_start(PMSG_RESTORE);
11881196 Resume_devices:
11891197 dpm_resume_end(PMSG_RESTORE);
....@@ -1196,7 +1204,7 @@
11961204 #endif
11971205
11981206 Unlock:
1199
- mutex_unlock(&kexec_mutex);
1207
+ kexec_unlock();
12001208 return error;
12011209 }
12021210