kexec_core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kexec.c - kexec system call core code.
  4. * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/capability.h>
  8. #include <linux/mm.h>
  9. #include <linux/file.h>
  10. #include <linux/slab.h>
  11. #include <linux/fs.h>
  12. #include <linux/kexec.h>
  13. #include <linux/mutex.h>
  14. #include <linux/list.h>
  15. #include <linux/highmem.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/reboot.h>
  18. #include <linux/ioport.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/elf.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/utsname.h>
  23. #include <linux/numa.h>
  24. #include <linux/suspend.h>
  25. #include <linux/device.h>
  26. #include <linux/freezer.h>
  27. #include <linux/pm.h>
  28. #include <linux/cpu.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/io.h>
  31. #include <linux/console.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/swap.h>
  34. #include <linux/syscore_ops.h>
  35. #include <linux/compiler.h>
  36. #include <linux/hugetlb.h>
  37. #include <linux/objtool.h>
  38. #include <asm/page.h>
  39. #include <asm/sections.h>
  40. #include <crypto/hash.h>
  41. #include <crypto/sha.h>
  42. #include "kexec_internal.h"
  43. DEFINE_MUTEX(kexec_mutex);
  44. /* Per cpu memory for storing cpu states in case of system crash. */
  45. note_buf_t __percpu *crash_notes;
  46. /* Flag to indicate we are going to kexec a new kernel */
  47. bool kexec_in_progress = false;
  48. /* Location of the reserved area for the crash kernel */
  49. struct resource crashk_res = {
  50. .name = "Crash kernel",
  51. .start = 0,
  52. .end = 0,
  53. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  54. .desc = IORES_DESC_CRASH_KERNEL
  55. };
  56. struct resource crashk_low_res = {
  57. .name = "Crash kernel",
  58. .start = 0,
  59. .end = 0,
  60. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  61. .desc = IORES_DESC_CRASH_KERNEL
  62. };
  63. int kexec_should_crash(struct task_struct *p)
  64. {
  65. /*
  66. * If crash_kexec_post_notifiers is enabled, don't run
  67. * crash_kexec() here yet, which must be run after panic
  68. * notifiers in panic().
  69. */
  70. if (crash_kexec_post_notifiers)
  71. return 0;
  72. /*
  73. * There are 4 panic() calls in do_exit() path, each of which
  74. * corresponds to each of these 4 conditions.
  75. */
  76. if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
  77. return 1;
  78. return 0;
  79. }
  80. int kexec_crash_loaded(void)
  81. {
  82. return !!kexec_crash_image;
  83. }
  84. EXPORT_SYMBOL_GPL(kexec_crash_loaded);
  85. /*
  86. * When kexec transitions to the new kernel there is a one-to-one
  87. * mapping between physical and virtual addresses. On processors
  88. * where you can disable the MMU this is trivial, and easy. For
  89. * others it is still a simple predictable page table to setup.
  90. *
  91. * In that environment kexec copies the new kernel to its final
  92. * resting place. This means I can only support memory whose
  93. * physical address can fit in an unsigned long. In particular
  94. * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
  95. * If the assembly stub has more restrictive requirements
  96. * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
  97. * defined more restrictively in <asm/kexec.h>.
  98. *
  99. * The code for the transition from the current kernel to the
  100. * new kernel is placed in the control_code_buffer, whose size
  101. * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
  102. * page of memory is necessary, but some architectures require more.
  103. * Because this memory must be identity mapped in the transition from
  104. * virtual to physical addresses it must live in the range
  105. * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
  106. * modifiable.
  107. *
  108. * The assembly stub in the control code buffer is passed a linked list
  109. * of descriptor pages detailing the source pages of the new kernel,
  110. * and the destination addresses of those source pages. As this data
  111. * structure is not used in the context of the current OS, it must
  112. * be self-contained.
  113. *
  114. * The code has been made to work with highmem pages and will use a
  115. * destination page in its final resting place (if it happens
  116. * to allocate it). The end product of this is that most of the
  117. * physical address space, and most of RAM can be used.
  118. *
  119. * Future directions include:
  120. * - allocating a page table with the control code buffer identity
  121. * mapped, to simplify machine_kexec and make kexec_on_panic more
  122. * reliable.
  123. */
  124. /*
  125. * KIMAGE_NO_DEST is an impossible destination address..., for
  126. * allocating pages whose destination address we do not care about.
  127. */
  128. #define KIMAGE_NO_DEST (-1UL)
  129. #define PAGE_COUNT(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
  130. static struct page *kimage_alloc_page(struct kimage *image,
  131. gfp_t gfp_mask,
  132. unsigned long dest);
  133. int sanity_check_segment_list(struct kimage *image)
  134. {
  135. int i;
  136. unsigned long nr_segments = image->nr_segments;
  137. unsigned long total_pages = 0;
  138. unsigned long nr_pages = totalram_pages();
  139. /*
  140. * Verify we have good destination addresses. The caller is
  141. * responsible for making certain we don't attempt to load
  142. * the new image into invalid or reserved areas of RAM. This
  143. * just verifies it is an address we can use.
  144. *
  145. * Since the kernel does everything in page size chunks ensure
  146. * the destination addresses are page aligned. Too many
  147. * special cases crop of when we don't do this. The most
  148. * insidious is getting overlapping destination addresses
  149. * simply because addresses are changed to page size
  150. * granularity.
  151. */
  152. for (i = 0; i < nr_segments; i++) {
  153. unsigned long mstart, mend;
  154. mstart = image->segment[i].mem;
  155. mend = mstart + image->segment[i].memsz;
  156. if (mstart > mend)
  157. return -EADDRNOTAVAIL;
  158. if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
  159. return -EADDRNOTAVAIL;
  160. if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
  161. return -EADDRNOTAVAIL;
  162. }
  163. /* Verify our destination addresses do not overlap.
  164. * If we alloed overlapping destination addresses
  165. * through very weird things can happen with no
  166. * easy explanation as one segment stops on another.
  167. */
  168. for (i = 0; i < nr_segments; i++) {
  169. unsigned long mstart, mend;
  170. unsigned long j;
  171. mstart = image->segment[i].mem;
  172. mend = mstart + image->segment[i].memsz;
  173. for (j = 0; j < i; j++) {
  174. unsigned long pstart, pend;
  175. pstart = image->segment[j].mem;
  176. pend = pstart + image->segment[j].memsz;
  177. /* Do the segments overlap ? */
  178. if ((mend > pstart) && (mstart < pend))
  179. return -EINVAL;
  180. }
  181. }
  182. /* Ensure our buffer sizes are strictly less than
  183. * our memory sizes. This should always be the case,
  184. * and it is easier to check up front than to be surprised
  185. * later on.
  186. */
  187. for (i = 0; i < nr_segments; i++) {
  188. if (image->segment[i].bufsz > image->segment[i].memsz)
  189. return -EINVAL;
  190. }
  191. /*
  192. * Verify that no more than half of memory will be consumed. If the
  193. * request from userspace is too large, a large amount of time will be
  194. * wasted allocating pages, which can cause a soft lockup.
  195. */
  196. for (i = 0; i < nr_segments; i++) {
  197. if (PAGE_COUNT(image->segment[i].memsz) > nr_pages / 2)
  198. return -EINVAL;
  199. total_pages += PAGE_COUNT(image->segment[i].memsz);
  200. }
  201. if (total_pages > nr_pages / 2)
  202. return -EINVAL;
  203. /*
  204. * Verify we have good destination addresses. Normally
  205. * the caller is responsible for making certain we don't
  206. * attempt to load the new image into invalid or reserved
  207. * areas of RAM. But crash kernels are preloaded into a
  208. * reserved area of ram. We must ensure the addresses
  209. * are in the reserved area otherwise preloading the
  210. * kernel could corrupt things.
  211. */
  212. if (image->type == KEXEC_TYPE_CRASH) {
  213. for (i = 0; i < nr_segments; i++) {
  214. unsigned long mstart, mend;
  215. mstart = image->segment[i].mem;
  216. mend = mstart + image->segment[i].memsz - 1;
  217. /* Ensure we are within the crash kernel limits */
  218. if ((mstart < phys_to_boot_phys(crashk_res.start)) ||
  219. (mend > phys_to_boot_phys(crashk_res.end)))
  220. return -EADDRNOTAVAIL;
  221. }
  222. }
  223. return 0;
  224. }
  225. struct kimage *do_kimage_alloc_init(void)
  226. {
  227. struct kimage *image;
  228. /* Allocate a controlling structure */
  229. image = kzalloc(sizeof(*image), GFP_KERNEL);
  230. if (!image)
  231. return NULL;
  232. image->head = 0;
  233. image->entry = &image->head;
  234. image->last_entry = &image->head;
  235. image->control_page = ~0; /* By default this does not apply */
  236. image->type = KEXEC_TYPE_DEFAULT;
  237. /* Initialize the list of control pages */
  238. INIT_LIST_HEAD(&image->control_pages);
  239. /* Initialize the list of destination pages */
  240. INIT_LIST_HEAD(&image->dest_pages);
  241. /* Initialize the list of unusable pages */
  242. INIT_LIST_HEAD(&image->unusable_pages);
  243. return image;
  244. }
  245. int kimage_is_destination_range(struct kimage *image,
  246. unsigned long start,
  247. unsigned long end)
  248. {
  249. unsigned long i;
  250. for (i = 0; i < image->nr_segments; i++) {
  251. unsigned long mstart, mend;
  252. mstart = image->segment[i].mem;
  253. mend = mstart + image->segment[i].memsz;
  254. if ((end > mstart) && (start < mend))
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
  260. {
  261. struct page *pages;
  262. if (fatal_signal_pending(current))
  263. return NULL;
  264. pages = alloc_pages(gfp_mask & ~__GFP_ZERO, order);
  265. if (pages) {
  266. unsigned int count, i;
  267. pages->mapping = NULL;
  268. set_page_private(pages, order);
  269. count = 1 << order;
  270. for (i = 0; i < count; i++)
  271. SetPageReserved(pages + i);
  272. arch_kexec_post_alloc_pages(page_address(pages), count,
  273. gfp_mask);
  274. if (gfp_mask & __GFP_ZERO)
  275. for (i = 0; i < count; i++)
  276. clear_highpage(pages + i);
  277. }
  278. return pages;
  279. }
  280. static void kimage_free_pages(struct page *page)
  281. {
  282. unsigned int order, count, i;
  283. order = page_private(page);
  284. count = 1 << order;
  285. arch_kexec_pre_free_pages(page_address(page), count);
  286. for (i = 0; i < count; i++)
  287. ClearPageReserved(page + i);
  288. __free_pages(page, order);
  289. }
  290. void kimage_free_page_list(struct list_head *list)
  291. {
  292. struct page *page, *next;
  293. list_for_each_entry_safe(page, next, list, lru) {
  294. list_del(&page->lru);
  295. kimage_free_pages(page);
  296. }
  297. }
  298. static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
  299. unsigned int order)
  300. {
  301. /* Control pages are special, they are the intermediaries
  302. * that are needed while we copy the rest of the pages
  303. * to their final resting place. As such they must
  304. * not conflict with either the destination addresses
  305. * or memory the kernel is already using.
  306. *
  307. * The only case where we really need more than one of
  308. * these are for architectures where we cannot disable
  309. * the MMU and must instead generate an identity mapped
  310. * page table for all of the memory.
  311. *
  312. * At worst this runs in O(N) of the image size.
  313. */
  314. struct list_head extra_pages;
  315. struct page *pages;
  316. unsigned int count;
  317. count = 1 << order;
  318. INIT_LIST_HEAD(&extra_pages);
  319. /* Loop while I can allocate a page and the page allocated
  320. * is a destination page.
  321. */
  322. do {
  323. unsigned long pfn, epfn, addr, eaddr;
  324. pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);
  325. if (!pages)
  326. break;
  327. pfn = page_to_boot_pfn(pages);
  328. epfn = pfn + count;
  329. addr = pfn << PAGE_SHIFT;
  330. eaddr = epfn << PAGE_SHIFT;
  331. if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
  332. kimage_is_destination_range(image, addr, eaddr)) {
  333. list_add(&pages->lru, &extra_pages);
  334. pages = NULL;
  335. }
  336. } while (!pages);
  337. if (pages) {
  338. /* Remember the allocated page... */
  339. list_add(&pages->lru, &image->control_pages);
  340. /* Because the page is already in it's destination
  341. * location we will never allocate another page at
  342. * that address. Therefore kimage_alloc_pages
  343. * will not return it (again) and we don't need
  344. * to give it an entry in image->segment[].
  345. */
  346. }
  347. /* Deal with the destination pages I have inadvertently allocated.
  348. *
  349. * Ideally I would convert multi-page allocations into single
  350. * page allocations, and add everything to image->dest_pages.
  351. *
  352. * For now it is simpler to just free the pages.
  353. */
  354. kimage_free_page_list(&extra_pages);
  355. return pages;
  356. }
  357. static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
  358. unsigned int order)
  359. {
  360. /* Control pages are special, they are the intermediaries
  361. * that are needed while we copy the rest of the pages
  362. * to their final resting place. As such they must
  363. * not conflict with either the destination addresses
  364. * or memory the kernel is already using.
  365. *
  366. * Control pages are also the only pags we must allocate
  367. * when loading a crash kernel. All of the other pages
  368. * are specified by the segments and we just memcpy
  369. * into them directly.
  370. *
  371. * The only case where we really need more than one of
  372. * these are for architectures where we cannot disable
  373. * the MMU and must instead generate an identity mapped
  374. * page table for all of the memory.
  375. *
  376. * Given the low demand this implements a very simple
  377. * allocator that finds the first hole of the appropriate
  378. * size in the reserved memory region, and allocates all
  379. * of the memory up to and including the hole.
  380. */
  381. unsigned long hole_start, hole_end, size;
  382. struct page *pages;
  383. pages = NULL;
  384. size = (1 << order) << PAGE_SHIFT;
  385. hole_start = (image->control_page + (size - 1)) & ~(size - 1);
  386. hole_end = hole_start + size - 1;
  387. while (hole_end <= crashk_res.end) {
  388. unsigned long i;
  389. cond_resched();
  390. if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
  391. break;
  392. /* See if I overlap any of the segments */
  393. for (i = 0; i < image->nr_segments; i++) {
  394. unsigned long mstart, mend;
  395. mstart = image->segment[i].mem;
  396. mend = mstart + image->segment[i].memsz - 1;
  397. if ((hole_end >= mstart) && (hole_start <= mend)) {
  398. /* Advance the hole to the end of the segment */
  399. hole_start = (mend + (size - 1)) & ~(size - 1);
  400. hole_end = hole_start + size - 1;
  401. break;
  402. }
  403. }
  404. /* If I don't overlap any segments I have found my hole! */
  405. if (i == image->nr_segments) {
  406. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  407. image->control_page = hole_end;
  408. break;
  409. }
  410. }
  411. /* Ensure that these pages are decrypted if SME is enabled. */
  412. if (pages)
  413. arch_kexec_post_alloc_pages(page_address(pages), 1 << order, 0);
  414. return pages;
  415. }
  416. struct page *kimage_alloc_control_pages(struct kimage *image,
  417. unsigned int order)
  418. {
  419. struct page *pages = NULL;
  420. switch (image->type) {
  421. case KEXEC_TYPE_DEFAULT:
  422. pages = kimage_alloc_normal_control_pages(image, order);
  423. break;
  424. case KEXEC_TYPE_CRASH:
  425. pages = kimage_alloc_crash_control_pages(image, order);
  426. break;
  427. }
  428. return pages;
  429. }
  430. int kimage_crash_copy_vmcoreinfo(struct kimage *image)
  431. {
  432. struct page *vmcoreinfo_page;
  433. void *safecopy;
  434. if (image->type != KEXEC_TYPE_CRASH)
  435. return 0;
  436. /*
  437. * For kdump, allocate one vmcoreinfo safe copy from the
  438. * crash memory. as we have arch_kexec_protect_crashkres()
  439. * after kexec syscall, we naturally protect it from write
  440. * (even read) access under kernel direct mapping. But on
  441. * the other hand, we still need to operate it when crash
  442. * happens to generate vmcoreinfo note, hereby we rely on
  443. * vmap for this purpose.
  444. */
  445. vmcoreinfo_page = kimage_alloc_control_pages(image, 0);
  446. if (!vmcoreinfo_page) {
  447. pr_warn("Could not allocate vmcoreinfo buffer\n");
  448. return -ENOMEM;
  449. }
  450. safecopy = vmap(&vmcoreinfo_page, 1, VM_MAP, PAGE_KERNEL);
  451. if (!safecopy) {
  452. pr_warn("Could not vmap vmcoreinfo buffer\n");
  453. return -ENOMEM;
  454. }
  455. image->vmcoreinfo_data_copy = safecopy;
  456. crash_update_vmcoreinfo_safecopy(safecopy);
  457. return 0;
  458. }
  459. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  460. {
  461. if (*image->entry != 0)
  462. image->entry++;
  463. if (image->entry == image->last_entry) {
  464. kimage_entry_t *ind_page;
  465. struct page *page;
  466. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  467. if (!page)
  468. return -ENOMEM;
  469. ind_page = page_address(page);
  470. *image->entry = virt_to_boot_phys(ind_page) | IND_INDIRECTION;
  471. image->entry = ind_page;
  472. image->last_entry = ind_page +
  473. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  474. }
  475. *image->entry = entry;
  476. image->entry++;
  477. *image->entry = 0;
  478. return 0;
  479. }
  480. static int kimage_set_destination(struct kimage *image,
  481. unsigned long destination)
  482. {
  483. int result;
  484. destination &= PAGE_MASK;
  485. result = kimage_add_entry(image, destination | IND_DESTINATION);
  486. return result;
  487. }
  488. static int kimage_add_page(struct kimage *image, unsigned long page)
  489. {
  490. int result;
  491. page &= PAGE_MASK;
  492. result = kimage_add_entry(image, page | IND_SOURCE);
  493. return result;
  494. }
  495. static void kimage_free_extra_pages(struct kimage *image)
  496. {
  497. /* Walk through and free any extra destination pages I may have */
  498. kimage_free_page_list(&image->dest_pages);
  499. /* Walk through and free any unusable pages I have cached */
  500. kimage_free_page_list(&image->unusable_pages);
  501. }
  502. int __weak machine_kexec_post_load(struct kimage *image)
  503. {
  504. return 0;
  505. }
  506. void kimage_terminate(struct kimage *image)
  507. {
  508. if (*image->entry != 0)
  509. image->entry++;
  510. *image->entry = IND_DONE;
  511. }
  512. #define for_each_kimage_entry(image, ptr, entry) \
  513. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  514. ptr = (entry & IND_INDIRECTION) ? \
  515. boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  516. static void kimage_free_entry(kimage_entry_t entry)
  517. {
  518. struct page *page;
  519. page = boot_pfn_to_page(entry >> PAGE_SHIFT);
  520. kimage_free_pages(page);
  521. }
  522. void kimage_free(struct kimage *image)
  523. {
  524. kimage_entry_t *ptr, entry;
  525. kimage_entry_t ind = 0;
  526. if (!image)
  527. return;
  528. if (image->vmcoreinfo_data_copy) {
  529. crash_update_vmcoreinfo_safecopy(NULL);
  530. vunmap(image->vmcoreinfo_data_copy);
  531. }
  532. kimage_free_extra_pages(image);
  533. for_each_kimage_entry(image, ptr, entry) {
  534. if (entry & IND_INDIRECTION) {
  535. /* Free the previous indirection page */
  536. if (ind & IND_INDIRECTION)
  537. kimage_free_entry(ind);
  538. /* Save this indirection page until we are
  539. * done with it.
  540. */
  541. ind = entry;
  542. } else if (entry & IND_SOURCE)
  543. kimage_free_entry(entry);
  544. }
  545. /* Free the final indirection page */
  546. if (ind & IND_INDIRECTION)
  547. kimage_free_entry(ind);
  548. /* Handle any machine specific cleanup */
  549. machine_kexec_cleanup(image);
  550. /* Free the kexec control pages... */
  551. kimage_free_page_list(&image->control_pages);
  552. /*
  553. * Free up any temporary buffers allocated. This might hit if
  554. * error occurred much later after buffer allocation.
  555. */
  556. if (image->file_mode)
  557. kimage_file_post_load_cleanup(image);
  558. kfree(image);
  559. }
  560. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  561. unsigned long page)
  562. {
  563. kimage_entry_t *ptr, entry;
  564. unsigned long destination = 0;
  565. for_each_kimage_entry(image, ptr, entry) {
  566. if (entry & IND_DESTINATION)
  567. destination = entry & PAGE_MASK;
  568. else if (entry & IND_SOURCE) {
  569. if (page == destination)
  570. return ptr;
  571. destination += PAGE_SIZE;
  572. }
  573. }
  574. return NULL;
  575. }
  576. static struct page *kimage_alloc_page(struct kimage *image,
  577. gfp_t gfp_mask,
  578. unsigned long destination)
  579. {
  580. /*
  581. * Here we implement safeguards to ensure that a source page
  582. * is not copied to its destination page before the data on
  583. * the destination page is no longer useful.
  584. *
  585. * To do this we maintain the invariant that a source page is
  586. * either its own destination page, or it is not a
  587. * destination page at all.
  588. *
  589. * That is slightly stronger than required, but the proof
  590. * that no problems will not occur is trivial, and the
  591. * implementation is simply to verify.
  592. *
  593. * When allocating all pages normally this algorithm will run
  594. * in O(N) time, but in the worst case it will run in O(N^2)
  595. * time. If the runtime is a problem the data structures can
  596. * be fixed.
  597. */
  598. struct page *page;
  599. unsigned long addr;
  600. /*
  601. * Walk through the list of destination pages, and see if I
  602. * have a match.
  603. */
  604. list_for_each_entry(page, &image->dest_pages, lru) {
  605. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  606. if (addr == destination) {
  607. list_del(&page->lru);
  608. return page;
  609. }
  610. }
  611. page = NULL;
  612. while (1) {
  613. kimage_entry_t *old;
  614. /* Allocate a page, if we run out of memory give up */
  615. page = kimage_alloc_pages(gfp_mask, 0);
  616. if (!page)
  617. return NULL;
  618. /* If the page cannot be used file it away */
  619. if (page_to_boot_pfn(page) >
  620. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  621. list_add(&page->lru, &image->unusable_pages);
  622. continue;
  623. }
  624. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  625. /* If it is the destination page we want use it */
  626. if (addr == destination)
  627. break;
  628. /* If the page is not a destination page use it */
  629. if (!kimage_is_destination_range(image, addr,
  630. addr + PAGE_SIZE))
  631. break;
  632. /*
  633. * I know that the page is someones destination page.
  634. * See if there is already a source page for this
  635. * destination page. And if so swap the source pages.
  636. */
  637. old = kimage_dst_used(image, addr);
  638. if (old) {
  639. /* If so move it */
  640. unsigned long old_addr;
  641. struct page *old_page;
  642. old_addr = *old & PAGE_MASK;
  643. old_page = boot_pfn_to_page(old_addr >> PAGE_SHIFT);
  644. copy_highpage(page, old_page);
  645. *old = addr | (*old & ~PAGE_MASK);
  646. /* The old page I have found cannot be a
  647. * destination page, so return it if it's
  648. * gfp_flags honor the ones passed in.
  649. */
  650. if (!(gfp_mask & __GFP_HIGHMEM) &&
  651. PageHighMem(old_page)) {
  652. kimage_free_pages(old_page);
  653. continue;
  654. }
  655. addr = old_addr;
  656. page = old_page;
  657. break;
  658. }
  659. /* Place the page on the destination list, to be used later */
  660. list_add(&page->lru, &image->dest_pages);
  661. }
  662. return page;
  663. }
  664. static int kimage_load_normal_segment(struct kimage *image,
  665. struct kexec_segment *segment)
  666. {
  667. unsigned long maddr;
  668. size_t ubytes, mbytes;
  669. int result;
  670. unsigned char __user *buf = NULL;
  671. unsigned char *kbuf = NULL;
  672. result = 0;
  673. if (image->file_mode)
  674. kbuf = segment->kbuf;
  675. else
  676. buf = segment->buf;
  677. ubytes = segment->bufsz;
  678. mbytes = segment->memsz;
  679. maddr = segment->mem;
  680. result = kimage_set_destination(image, maddr);
  681. if (result < 0)
  682. goto out;
  683. while (mbytes) {
  684. struct page *page;
  685. char *ptr;
  686. size_t uchunk, mchunk;
  687. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  688. if (!page) {
  689. result = -ENOMEM;
  690. goto out;
  691. }
  692. result = kimage_add_page(image, page_to_boot_pfn(page)
  693. << PAGE_SHIFT);
  694. if (result < 0)
  695. goto out;
  696. ptr = kmap(page);
  697. /* Start with a clear page */
  698. clear_page(ptr);
  699. ptr += maddr & ~PAGE_MASK;
  700. mchunk = min_t(size_t, mbytes,
  701. PAGE_SIZE - (maddr & ~PAGE_MASK));
  702. uchunk = min(ubytes, mchunk);
  703. /* For file based kexec, source pages are in kernel memory */
  704. if (image->file_mode)
  705. memcpy(ptr, kbuf, uchunk);
  706. else
  707. result = copy_from_user(ptr, buf, uchunk);
  708. kunmap(page);
  709. if (result) {
  710. result = -EFAULT;
  711. goto out;
  712. }
  713. ubytes -= uchunk;
  714. maddr += mchunk;
  715. if (image->file_mode)
  716. kbuf += mchunk;
  717. else
  718. buf += mchunk;
  719. mbytes -= mchunk;
  720. cond_resched();
  721. }
  722. out:
  723. return result;
  724. }
  725. static int kimage_load_crash_segment(struct kimage *image,
  726. struct kexec_segment *segment)
  727. {
  728. /* For crash dumps kernels we simply copy the data from
  729. * user space to it's destination.
  730. * We do things a page at a time for the sake of kmap.
  731. */
  732. unsigned long maddr;
  733. size_t ubytes, mbytes;
  734. int result;
  735. unsigned char __user *buf = NULL;
  736. unsigned char *kbuf = NULL;
  737. result = 0;
  738. if (image->file_mode)
  739. kbuf = segment->kbuf;
  740. else
  741. buf = segment->buf;
  742. ubytes = segment->bufsz;
  743. mbytes = segment->memsz;
  744. maddr = segment->mem;
  745. while (mbytes) {
  746. struct page *page;
  747. char *ptr;
  748. size_t uchunk, mchunk;
  749. page = boot_pfn_to_page(maddr >> PAGE_SHIFT);
  750. if (!page) {
  751. result = -ENOMEM;
  752. goto out;
  753. }
  754. arch_kexec_post_alloc_pages(page_address(page), 1, 0);
  755. ptr = kmap(page);
  756. ptr += maddr & ~PAGE_MASK;
  757. mchunk = min_t(size_t, mbytes,
  758. PAGE_SIZE - (maddr & ~PAGE_MASK));
  759. uchunk = min(ubytes, mchunk);
  760. if (mchunk > uchunk) {
  761. /* Zero the trailing part of the page */
  762. memset(ptr + uchunk, 0, mchunk - uchunk);
  763. }
  764. /* For file based kexec, source pages are in kernel memory */
  765. if (image->file_mode)
  766. memcpy(ptr, kbuf, uchunk);
  767. else
  768. result = copy_from_user(ptr, buf, uchunk);
  769. kexec_flush_icache_page(page);
  770. kunmap(page);
  771. arch_kexec_pre_free_pages(page_address(page), 1);
  772. if (result) {
  773. result = -EFAULT;
  774. goto out;
  775. }
  776. ubytes -= uchunk;
  777. maddr += mchunk;
  778. if (image->file_mode)
  779. kbuf += mchunk;
  780. else
  781. buf += mchunk;
  782. mbytes -= mchunk;
  783. cond_resched();
  784. }
  785. out:
  786. return result;
  787. }
  788. int kimage_load_segment(struct kimage *image,
  789. struct kexec_segment *segment)
  790. {
  791. int result = -ENOMEM;
  792. switch (image->type) {
  793. case KEXEC_TYPE_DEFAULT:
  794. result = kimage_load_normal_segment(image, segment);
  795. break;
  796. case KEXEC_TYPE_CRASH:
  797. result = kimage_load_crash_segment(image, segment);
  798. break;
  799. }
  800. return result;
  801. }
  802. struct kimage *kexec_image;
  803. struct kimage *kexec_crash_image;
  804. int kexec_load_disabled;
  805. /*
  806. * No panic_cpu check version of crash_kexec(). This function is called
  807. * only when panic_cpu holds the current CPU number; this is the only CPU
  808. * which processes crash_kexec routines.
  809. */
  810. void __noclone __crash_kexec(struct pt_regs *regs)
  811. {
  812. /* Take the kexec_mutex here to prevent sys_kexec_load
  813. * running on one cpu from replacing the crash kernel
  814. * we are using after a panic on a different cpu.
  815. *
  816. * If the crash kernel was not located in a fixed area
  817. * of memory the xchg(&kexec_crash_image) would be
  818. * sufficient. But since I reuse the memory...
  819. */
  820. if (mutex_trylock(&kexec_mutex)) {
  821. if (kexec_crash_image) {
  822. struct pt_regs fixed_regs;
  823. crash_setup_regs(&fixed_regs, regs);
  824. crash_save_vmcoreinfo();
  825. machine_crash_shutdown(&fixed_regs);
  826. machine_kexec(kexec_crash_image);
  827. }
  828. mutex_unlock(&kexec_mutex);
  829. }
  830. }
  831. STACK_FRAME_NON_STANDARD(__crash_kexec);
  832. void crash_kexec(struct pt_regs *regs)
  833. {
  834. int old_cpu, this_cpu;
  835. /*
  836. * Only one CPU is allowed to execute the crash_kexec() code as with
  837. * panic(). Otherwise parallel calls of panic() and crash_kexec()
  838. * may stop each other. To exclude them, we use panic_cpu here too.
  839. */
  840. this_cpu = raw_smp_processor_id();
  841. old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  842. if (old_cpu == PANIC_CPU_INVALID) {
  843. /* This is the 1st CPU which comes here, so go ahead. */
  844. printk_safe_flush_on_panic();
  845. __crash_kexec(regs);
  846. /*
  847. * Reset panic_cpu to allow another panic()/crash_kexec()
  848. * call.
  849. */
  850. atomic_set(&panic_cpu, PANIC_CPU_INVALID);
  851. }
  852. }
  853. size_t crash_get_memory_size(void)
  854. {
  855. size_t size = 0;
  856. mutex_lock(&kexec_mutex);
  857. if (crashk_res.end != crashk_res.start)
  858. size = resource_size(&crashk_res);
  859. mutex_unlock(&kexec_mutex);
  860. return size;
  861. }
  862. void __weak crash_free_reserved_phys_range(unsigned long begin,
  863. unsigned long end)
  864. {
  865. unsigned long addr;
  866. for (addr = begin; addr < end; addr += PAGE_SIZE)
  867. free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
  868. }
  869. int crash_shrink_memory(unsigned long new_size)
  870. {
  871. int ret = 0;
  872. unsigned long start, end;
  873. unsigned long old_size;
  874. struct resource *ram_res;
  875. mutex_lock(&kexec_mutex);
  876. if (kexec_crash_image) {
  877. ret = -ENOENT;
  878. goto unlock;
  879. }
  880. start = crashk_res.start;
  881. end = crashk_res.end;
  882. old_size = (end == 0) ? 0 : end - start + 1;
  883. if (new_size >= old_size) {
  884. ret = (new_size == old_size) ? 0 : -EINVAL;
  885. goto unlock;
  886. }
  887. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  888. if (!ram_res) {
  889. ret = -ENOMEM;
  890. goto unlock;
  891. }
  892. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  893. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  894. crash_free_reserved_phys_range(end, crashk_res.end);
  895. if ((start == end) && (crashk_res.parent != NULL))
  896. release_resource(&crashk_res);
  897. ram_res->start = end;
  898. ram_res->end = crashk_res.end;
  899. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  900. ram_res->name = "System RAM";
  901. crashk_res.end = end - 1;
  902. insert_resource(&iomem_resource, ram_res);
  903. unlock:
  904. mutex_unlock(&kexec_mutex);
  905. return ret;
  906. }
  907. void crash_save_cpu(struct pt_regs *regs, int cpu)
  908. {
  909. struct elf_prstatus prstatus;
  910. u32 *buf;
  911. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  912. return;
  913. /* Using ELF notes here is opportunistic.
  914. * I need a well defined structure format
  915. * for the data I pass, and I need tags
  916. * on the data to indicate what information I have
  917. * squirrelled away. ELF notes happen to provide
  918. * all of that, so there is no need to invent something new.
  919. */
  920. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  921. if (!buf)
  922. return;
  923. memset(&prstatus, 0, sizeof(prstatus));
  924. prstatus.pr_pid = current->pid;
  925. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  926. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  927. &prstatus, sizeof(prstatus));
  928. final_note(buf);
  929. }
  930. static int __init crash_notes_memory_init(void)
  931. {
  932. /* Allocate memory for saving cpu registers. */
  933. size_t size, align;
  934. /*
  935. * crash_notes could be allocated across 2 vmalloc pages when percpu
  936. * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
  937. * pages are also on 2 continuous physical pages. In this case the
  938. * 2nd part of crash_notes in 2nd page could be lost since only the
  939. * starting address and size of crash_notes are exported through sysfs.
  940. * Here round up the size of crash_notes to the nearest power of two
  941. * and pass it to __alloc_percpu as align value. This can make sure
  942. * crash_notes is allocated inside one physical page.
  943. */
  944. size = sizeof(note_buf_t);
  945. align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
  946. /*
  947. * Break compile if size is bigger than PAGE_SIZE since crash_notes
  948. * definitely will be in 2 pages with that.
  949. */
  950. BUILD_BUG_ON(size > PAGE_SIZE);
  951. crash_notes = __alloc_percpu(size, align);
  952. if (!crash_notes) {
  953. pr_warn("Memory allocation for saving cpu register states failed\n");
  954. return -ENOMEM;
  955. }
  956. return 0;
  957. }
  958. subsys_initcall(crash_notes_memory_init);
  959. /*
  960. * Move into place and start executing a preloaded standalone
  961. * executable. If nothing was preloaded return an error.
  962. */
  963. int kernel_kexec(void)
  964. {
  965. int error = 0;
  966. if (!mutex_trylock(&kexec_mutex))
  967. return -EBUSY;
  968. if (!kexec_image) {
  969. error = -EINVAL;
  970. goto Unlock;
  971. }
  972. #ifdef CONFIG_KEXEC_JUMP
  973. if (kexec_image->preserve_context) {
  974. pm_prepare_console();
  975. error = freeze_processes();
  976. if (error) {
  977. error = -EBUSY;
  978. goto Restore_console;
  979. }
  980. suspend_console();
  981. error = dpm_suspend_start(PMSG_FREEZE);
  982. if (error)
  983. goto Resume_console;
  984. /* At this point, dpm_suspend_start() has been called,
  985. * but *not* dpm_suspend_end(). We *must* call
  986. * dpm_suspend_end() now. Otherwise, drivers for
  987. * some devices (e.g. interrupt controllers) become
  988. * desynchronized with the actual state of the
  989. * hardware at resume time, and evil weirdness ensues.
  990. */
  991. error = dpm_suspend_end(PMSG_FREEZE);
  992. if (error)
  993. goto Resume_devices;
  994. error = suspend_disable_secondary_cpus();
  995. if (error)
  996. goto Enable_cpus;
  997. local_irq_disable();
  998. error = syscore_suspend();
  999. if (error)
  1000. goto Enable_irqs;
  1001. } else
  1002. #endif
  1003. {
  1004. kexec_in_progress = true;
  1005. kernel_restart_prepare(NULL);
  1006. migrate_to_reboot_cpu();
  1007. /*
  1008. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  1009. * no further code needs to use CPU hotplug (which is true in
  1010. * the reboot case). However, the kexec path depends on using
  1011. * CPU hotplug again; so re-enable it here.
  1012. */
  1013. cpu_hotplug_enable();
  1014. pr_notice("Starting new kernel\n");
  1015. machine_shutdown();
  1016. }
  1017. machine_kexec(kexec_image);
  1018. #ifdef CONFIG_KEXEC_JUMP
  1019. if (kexec_image->preserve_context) {
  1020. syscore_resume();
  1021. Enable_irqs:
  1022. local_irq_enable();
  1023. Enable_cpus:
  1024. suspend_enable_secondary_cpus();
  1025. dpm_resume_start(PMSG_RESTORE);
  1026. Resume_devices:
  1027. dpm_resume_end(PMSG_RESTORE);
  1028. Resume_console:
  1029. resume_console();
  1030. thaw_processes();
  1031. Restore_console:
  1032. pm_restore_console();
  1033. }
  1034. #endif
  1035. Unlock:
  1036. mutex_unlock(&kexec_mutex);
  1037. return error;
  1038. }
  1039. /*
  1040. * Protection mechanism for crashkernel reserved memory after
  1041. * the kdump kernel is loaded.
  1042. *
  1043. * Provide an empty default implementation here -- architecture
  1044. * code may override this
  1045. */
  1046. void __weak arch_kexec_protect_crashkres(void)
  1047. {}
  1048. void __weak arch_kexec_unprotect_crashkres(void)
  1049. {}