kexec_core.c 30 KB

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