kexec_core.c 29 KB

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