kexec_core.c 31 KB

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