kexec_core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. int kimage_crash_copy_vmcoreinfo(struct kimage *image)
  421. {
  422. struct page *vmcoreinfo_page;
  423. void *safecopy;
  424. if (image->type != KEXEC_TYPE_CRASH)
  425. return 0;
  426. /*
  427. * For kdump, allocate one vmcoreinfo safe copy from the
  428. * crash memory. as we have arch_kexec_protect_crashkres()
  429. * after kexec syscall, we naturally protect it from write
  430. * (even read) access under kernel direct mapping. But on
  431. * the other hand, we still need to operate it when crash
  432. * happens to generate vmcoreinfo note, hereby we rely on
  433. * vmap for this purpose.
  434. */
  435. vmcoreinfo_page = kimage_alloc_control_pages(image, 0);
  436. if (!vmcoreinfo_page) {
  437. pr_warn("Could not allocate vmcoreinfo buffer\n");
  438. return -ENOMEM;
  439. }
  440. safecopy = vmap(&vmcoreinfo_page, 1, VM_MAP, PAGE_KERNEL);
  441. if (!safecopy) {
  442. pr_warn("Could not vmap vmcoreinfo buffer\n");
  443. return -ENOMEM;
  444. }
  445. image->vmcoreinfo_data_copy = safecopy;
  446. crash_update_vmcoreinfo_safecopy(safecopy);
  447. return 0;
  448. }
  449. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  450. {
  451. if (*image->entry != 0)
  452. image->entry++;
  453. if (image->entry == image->last_entry) {
  454. kimage_entry_t *ind_page;
  455. struct page *page;
  456. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  457. if (!page)
  458. return -ENOMEM;
  459. ind_page = page_address(page);
  460. *image->entry = virt_to_boot_phys(ind_page) | IND_INDIRECTION;
  461. image->entry = ind_page;
  462. image->last_entry = ind_page +
  463. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  464. }
  465. *image->entry = entry;
  466. image->entry++;
  467. *image->entry = 0;
  468. return 0;
  469. }
  470. static int kimage_set_destination(struct kimage *image,
  471. unsigned long destination)
  472. {
  473. int result;
  474. destination &= PAGE_MASK;
  475. result = kimage_add_entry(image, destination | IND_DESTINATION);
  476. return result;
  477. }
  478. static int kimage_add_page(struct kimage *image, unsigned long page)
  479. {
  480. int result;
  481. page &= PAGE_MASK;
  482. result = kimage_add_entry(image, page | IND_SOURCE);
  483. return result;
  484. }
  485. static void kimage_free_extra_pages(struct kimage *image)
  486. {
  487. /* Walk through and free any extra destination pages I may have */
  488. kimage_free_page_list(&image->dest_pages);
  489. /* Walk through and free any unusable pages I have cached */
  490. kimage_free_page_list(&image->unusable_pages);
  491. }
  492. void kimage_terminate(struct kimage *image)
  493. {
  494. if (*image->entry != 0)
  495. image->entry++;
  496. *image->entry = IND_DONE;
  497. }
  498. #define for_each_kimage_entry(image, ptr, entry) \
  499. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  500. ptr = (entry & IND_INDIRECTION) ? \
  501. boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  502. static void kimage_free_entry(kimage_entry_t entry)
  503. {
  504. struct page *page;
  505. page = boot_pfn_to_page(entry >> PAGE_SHIFT);
  506. kimage_free_pages(page);
  507. }
  508. void kimage_free(struct kimage *image)
  509. {
  510. kimage_entry_t *ptr, entry;
  511. kimage_entry_t ind = 0;
  512. if (!image)
  513. return;
  514. if (image->vmcoreinfo_data_copy) {
  515. crash_update_vmcoreinfo_safecopy(NULL);
  516. vunmap(image->vmcoreinfo_data_copy);
  517. }
  518. kimage_free_extra_pages(image);
  519. for_each_kimage_entry(image, ptr, entry) {
  520. if (entry & IND_INDIRECTION) {
  521. /* Free the previous indirection page */
  522. if (ind & IND_INDIRECTION)
  523. kimage_free_entry(ind);
  524. /* Save this indirection page until we are
  525. * done with it.
  526. */
  527. ind = entry;
  528. } else if (entry & IND_SOURCE)
  529. kimage_free_entry(entry);
  530. }
  531. /* Free the final indirection page */
  532. if (ind & IND_INDIRECTION)
  533. kimage_free_entry(ind);
  534. /* Handle any machine specific cleanup */
  535. machine_kexec_cleanup(image);
  536. /* Free the kexec control pages... */
  537. kimage_free_page_list(&image->control_pages);
  538. /*
  539. * Free up any temporary buffers allocated. This might hit if
  540. * error occurred much later after buffer allocation.
  541. */
  542. if (image->file_mode)
  543. kimage_file_post_load_cleanup(image);
  544. kfree(image);
  545. }
  546. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  547. unsigned long page)
  548. {
  549. kimage_entry_t *ptr, entry;
  550. unsigned long destination = 0;
  551. for_each_kimage_entry(image, ptr, entry) {
  552. if (entry & IND_DESTINATION)
  553. destination = entry & PAGE_MASK;
  554. else if (entry & IND_SOURCE) {
  555. if (page == destination)
  556. return ptr;
  557. destination += PAGE_SIZE;
  558. }
  559. }
  560. return NULL;
  561. }
  562. static struct page *kimage_alloc_page(struct kimage *image,
  563. gfp_t gfp_mask,
  564. unsigned long destination)
  565. {
  566. /*
  567. * Here we implement safeguards to ensure that a source page
  568. * is not copied to its destination page before the data on
  569. * the destination page is no longer useful.
  570. *
  571. * To do this we maintain the invariant that a source page is
  572. * either its own destination page, or it is not a
  573. * destination page at all.
  574. *
  575. * That is slightly stronger than required, but the proof
  576. * that no problems will not occur is trivial, and the
  577. * implementation is simply to verify.
  578. *
  579. * When allocating all pages normally this algorithm will run
  580. * in O(N) time, but in the worst case it will run in O(N^2)
  581. * time. If the runtime is a problem the data structures can
  582. * be fixed.
  583. */
  584. struct page *page;
  585. unsigned long addr;
  586. /*
  587. * Walk through the list of destination pages, and see if I
  588. * have a match.
  589. */
  590. list_for_each_entry(page, &image->dest_pages, lru) {
  591. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  592. if (addr == destination) {
  593. list_del(&page->lru);
  594. return page;
  595. }
  596. }
  597. page = NULL;
  598. while (1) {
  599. kimage_entry_t *old;
  600. /* Allocate a page, if we run out of memory give up */
  601. page = kimage_alloc_pages(gfp_mask, 0);
  602. if (!page)
  603. return NULL;
  604. /* If the page cannot be used file it away */
  605. if (page_to_boot_pfn(page) >
  606. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  607. list_add(&page->lru, &image->unusable_pages);
  608. continue;
  609. }
  610. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  611. /* If it is the destination page we want use it */
  612. if (addr == destination)
  613. break;
  614. /* If the page is not a destination page use it */
  615. if (!kimage_is_destination_range(image, addr,
  616. addr + PAGE_SIZE))
  617. break;
  618. /*
  619. * I know that the page is someones destination page.
  620. * See if there is already a source page for this
  621. * destination page. And if so swap the source pages.
  622. */
  623. old = kimage_dst_used(image, addr);
  624. if (old) {
  625. /* If so move it */
  626. unsigned long old_addr;
  627. struct page *old_page;
  628. old_addr = *old & PAGE_MASK;
  629. old_page = boot_pfn_to_page(old_addr >> PAGE_SHIFT);
  630. copy_highpage(page, old_page);
  631. *old = addr | (*old & ~PAGE_MASK);
  632. /* The old page I have found cannot be a
  633. * destination page, so return it if it's
  634. * gfp_flags honor the ones passed in.
  635. */
  636. if (!(gfp_mask & __GFP_HIGHMEM) &&
  637. PageHighMem(old_page)) {
  638. kimage_free_pages(old_page);
  639. continue;
  640. }
  641. addr = old_addr;
  642. page = old_page;
  643. break;
  644. }
  645. /* Place the page on the destination list, to be used later */
  646. list_add(&page->lru, &image->dest_pages);
  647. }
  648. return page;
  649. }
  650. static int kimage_load_normal_segment(struct kimage *image,
  651. struct kexec_segment *segment)
  652. {
  653. unsigned long maddr;
  654. size_t ubytes, mbytes;
  655. int result;
  656. unsigned char __user *buf = NULL;
  657. unsigned char *kbuf = NULL;
  658. result = 0;
  659. if (image->file_mode)
  660. kbuf = segment->kbuf;
  661. else
  662. buf = segment->buf;
  663. ubytes = segment->bufsz;
  664. mbytes = segment->memsz;
  665. maddr = segment->mem;
  666. result = kimage_set_destination(image, maddr);
  667. if (result < 0)
  668. goto out;
  669. while (mbytes) {
  670. struct page *page;
  671. char *ptr;
  672. size_t uchunk, mchunk;
  673. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  674. if (!page) {
  675. result = -ENOMEM;
  676. goto out;
  677. }
  678. result = kimage_add_page(image, page_to_boot_pfn(page)
  679. << PAGE_SHIFT);
  680. if (result < 0)
  681. goto out;
  682. ptr = kmap(page);
  683. /* Start with a clear page */
  684. clear_page(ptr);
  685. ptr += maddr & ~PAGE_MASK;
  686. mchunk = min_t(size_t, mbytes,
  687. PAGE_SIZE - (maddr & ~PAGE_MASK));
  688. uchunk = min(ubytes, mchunk);
  689. /* For file based kexec, source pages are in kernel memory */
  690. if (image->file_mode)
  691. memcpy(ptr, kbuf, uchunk);
  692. else
  693. result = copy_from_user(ptr, buf, uchunk);
  694. kunmap(page);
  695. if (result) {
  696. result = -EFAULT;
  697. goto out;
  698. }
  699. ubytes -= uchunk;
  700. maddr += mchunk;
  701. if (image->file_mode)
  702. kbuf += mchunk;
  703. else
  704. buf += mchunk;
  705. mbytes -= mchunk;
  706. }
  707. out:
  708. return result;
  709. }
  710. static int kimage_load_crash_segment(struct kimage *image,
  711. struct kexec_segment *segment)
  712. {
  713. /* For crash dumps kernels we simply copy the data from
  714. * user space to it's destination.
  715. * We do things a page at a time for the sake of kmap.
  716. */
  717. unsigned long maddr;
  718. size_t ubytes, mbytes;
  719. int result;
  720. unsigned char __user *buf = NULL;
  721. unsigned char *kbuf = NULL;
  722. result = 0;
  723. if (image->file_mode)
  724. kbuf = segment->kbuf;
  725. else
  726. buf = segment->buf;
  727. ubytes = segment->bufsz;
  728. mbytes = segment->memsz;
  729. maddr = segment->mem;
  730. while (mbytes) {
  731. struct page *page;
  732. char *ptr;
  733. size_t uchunk, mchunk;
  734. page = boot_pfn_to_page(maddr >> PAGE_SHIFT);
  735. if (!page) {
  736. result = -ENOMEM;
  737. goto out;
  738. }
  739. ptr = kmap(page);
  740. ptr += maddr & ~PAGE_MASK;
  741. mchunk = min_t(size_t, mbytes,
  742. PAGE_SIZE - (maddr & ~PAGE_MASK));
  743. uchunk = min(ubytes, mchunk);
  744. if (mchunk > uchunk) {
  745. /* Zero the trailing part of the page */
  746. memset(ptr + uchunk, 0, mchunk - uchunk);
  747. }
  748. /* For file based kexec, source pages are in kernel memory */
  749. if (image->file_mode)
  750. memcpy(ptr, kbuf, uchunk);
  751. else
  752. result = copy_from_user(ptr, buf, uchunk);
  753. kexec_flush_icache_page(page);
  754. kunmap(page);
  755. if (result) {
  756. result = -EFAULT;
  757. goto out;
  758. }
  759. ubytes -= uchunk;
  760. maddr += mchunk;
  761. if (image->file_mode)
  762. kbuf += mchunk;
  763. else
  764. buf += mchunk;
  765. mbytes -= mchunk;
  766. }
  767. out:
  768. return result;
  769. }
  770. int kimage_load_segment(struct kimage *image,
  771. struct kexec_segment *segment)
  772. {
  773. int result = -ENOMEM;
  774. switch (image->type) {
  775. case KEXEC_TYPE_DEFAULT:
  776. result = kimage_load_normal_segment(image, segment);
  777. break;
  778. case KEXEC_TYPE_CRASH:
  779. result = kimage_load_crash_segment(image, segment);
  780. break;
  781. }
  782. return result;
  783. }
  784. struct kimage *kexec_image;
  785. struct kimage *kexec_crash_image;
  786. int kexec_load_disabled;
  787. /*
  788. * No panic_cpu check version of crash_kexec(). This function is called
  789. * only when panic_cpu holds the current CPU number; this is the only CPU
  790. * which processes crash_kexec routines.
  791. */
  792. void __noclone __crash_kexec(struct pt_regs *regs)
  793. {
  794. /* Take the kexec_mutex here to prevent sys_kexec_load
  795. * running on one cpu from replacing the crash kernel
  796. * we are using after a panic on a different cpu.
  797. *
  798. * If the crash kernel was not located in a fixed area
  799. * of memory the xchg(&kexec_crash_image) would be
  800. * sufficient. But since I reuse the memory...
  801. */
  802. if (mutex_trylock(&kexec_mutex)) {
  803. if (kexec_crash_image) {
  804. struct pt_regs fixed_regs;
  805. crash_setup_regs(&fixed_regs, regs);
  806. crash_save_vmcoreinfo();
  807. machine_crash_shutdown(&fixed_regs);
  808. machine_kexec(kexec_crash_image);
  809. }
  810. mutex_unlock(&kexec_mutex);
  811. }
  812. }
  813. STACK_FRAME_NON_STANDARD(__crash_kexec);
  814. void crash_kexec(struct pt_regs *regs)
  815. {
  816. int old_cpu, this_cpu;
  817. /*
  818. * Only one CPU is allowed to execute the crash_kexec() code as with
  819. * panic(). Otherwise parallel calls of panic() and crash_kexec()
  820. * may stop each other. To exclude them, we use panic_cpu here too.
  821. */
  822. this_cpu = raw_smp_processor_id();
  823. old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  824. if (old_cpu == PANIC_CPU_INVALID) {
  825. /* This is the 1st CPU which comes here, so go ahead. */
  826. printk_safe_flush_on_panic();
  827. __crash_kexec(regs);
  828. /*
  829. * Reset panic_cpu to allow another panic()/crash_kexec()
  830. * call.
  831. */
  832. atomic_set(&panic_cpu, PANIC_CPU_INVALID);
  833. }
  834. }
  835. size_t crash_get_memory_size(void)
  836. {
  837. size_t size = 0;
  838. mutex_lock(&kexec_mutex);
  839. if (crashk_res.end != crashk_res.start)
  840. size = resource_size(&crashk_res);
  841. mutex_unlock(&kexec_mutex);
  842. return size;
  843. }
  844. void __weak crash_free_reserved_phys_range(unsigned long begin,
  845. unsigned long end)
  846. {
  847. unsigned long addr;
  848. for (addr = begin; addr < end; addr += PAGE_SIZE)
  849. free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
  850. }
  851. int crash_shrink_memory(unsigned long new_size)
  852. {
  853. int ret = 0;
  854. unsigned long start, end;
  855. unsigned long old_size;
  856. struct resource *ram_res;
  857. mutex_lock(&kexec_mutex);
  858. if (kexec_crash_image) {
  859. ret = -ENOENT;
  860. goto unlock;
  861. }
  862. start = crashk_res.start;
  863. end = crashk_res.end;
  864. old_size = (end == 0) ? 0 : end - start + 1;
  865. if (new_size >= old_size) {
  866. ret = (new_size == old_size) ? 0 : -EINVAL;
  867. goto unlock;
  868. }
  869. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  870. if (!ram_res) {
  871. ret = -ENOMEM;
  872. goto unlock;
  873. }
  874. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  875. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  876. crash_free_reserved_phys_range(end, crashk_res.end);
  877. if ((start == end) && (crashk_res.parent != NULL))
  878. release_resource(&crashk_res);
  879. ram_res->start = end;
  880. ram_res->end = crashk_res.end;
  881. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  882. ram_res->name = "System RAM";
  883. crashk_res.end = end - 1;
  884. insert_resource(&iomem_resource, ram_res);
  885. unlock:
  886. mutex_unlock(&kexec_mutex);
  887. return ret;
  888. }
  889. void crash_save_cpu(struct pt_regs *regs, int cpu)
  890. {
  891. struct elf_prstatus prstatus;
  892. u32 *buf;
  893. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  894. return;
  895. /* Using ELF notes here is opportunistic.
  896. * I need a well defined structure format
  897. * for the data I pass, and I need tags
  898. * on the data to indicate what information I have
  899. * squirrelled away. ELF notes happen to provide
  900. * all of that, so there is no need to invent something new.
  901. */
  902. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  903. if (!buf)
  904. return;
  905. memset(&prstatus, 0, sizeof(prstatus));
  906. prstatus.pr_pid = current->pid;
  907. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  908. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  909. &prstatus, sizeof(prstatus));
  910. final_note(buf);
  911. }
  912. static int __init crash_notes_memory_init(void)
  913. {
  914. /* Allocate memory for saving cpu registers. */
  915. size_t size, align;
  916. /*
  917. * crash_notes could be allocated across 2 vmalloc pages when percpu
  918. * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
  919. * pages are also on 2 continuous physical pages. In this case the
  920. * 2nd part of crash_notes in 2nd page could be lost since only the
  921. * starting address and size of crash_notes are exported through sysfs.
  922. * Here round up the size of crash_notes to the nearest power of two
  923. * and pass it to __alloc_percpu as align value. This can make sure
  924. * crash_notes is allocated inside one physical page.
  925. */
  926. size = sizeof(note_buf_t);
  927. align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
  928. /*
  929. * Break compile if size is bigger than PAGE_SIZE since crash_notes
  930. * definitely will be in 2 pages with that.
  931. */
  932. BUILD_BUG_ON(size > PAGE_SIZE);
  933. crash_notes = __alloc_percpu(size, align);
  934. if (!crash_notes) {
  935. pr_warn("Memory allocation for saving cpu register states failed\n");
  936. return -ENOMEM;
  937. }
  938. return 0;
  939. }
  940. subsys_initcall(crash_notes_memory_init);
  941. /*
  942. * Move into place and start executing a preloaded standalone
  943. * executable. If nothing was preloaded return an error.
  944. */
  945. int kernel_kexec(void)
  946. {
  947. int error = 0;
  948. if (!mutex_trylock(&kexec_mutex))
  949. return -EBUSY;
  950. if (!kexec_image) {
  951. error = -EINVAL;
  952. goto Unlock;
  953. }
  954. #ifdef CONFIG_KEXEC_JUMP
  955. if (kexec_image->preserve_context) {
  956. lock_system_sleep();
  957. pm_prepare_console();
  958. error = freeze_processes();
  959. if (error) {
  960. error = -EBUSY;
  961. goto Restore_console;
  962. }
  963. suspend_console();
  964. error = dpm_suspend_start(PMSG_FREEZE);
  965. if (error)
  966. goto Resume_console;
  967. /* At this point, dpm_suspend_start() has been called,
  968. * but *not* dpm_suspend_end(). We *must* call
  969. * dpm_suspend_end() now. Otherwise, drivers for
  970. * some devices (e.g. interrupt controllers) become
  971. * desynchronized with the actual state of the
  972. * hardware at resume time, and evil weirdness ensues.
  973. */
  974. error = dpm_suspend_end(PMSG_FREEZE);
  975. if (error)
  976. goto Resume_devices;
  977. error = disable_nonboot_cpus();
  978. if (error)
  979. goto Enable_cpus;
  980. local_irq_disable();
  981. error = syscore_suspend();
  982. if (error)
  983. goto Enable_irqs;
  984. } else
  985. #endif
  986. {
  987. kexec_in_progress = true;
  988. kernel_restart_prepare(NULL);
  989. migrate_to_reboot_cpu();
  990. /*
  991. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  992. * no further code needs to use CPU hotplug (which is true in
  993. * the reboot case). However, the kexec path depends on using
  994. * CPU hotplug again; so re-enable it here.
  995. */
  996. cpu_hotplug_enable();
  997. pr_emerg("Starting new kernel\n");
  998. machine_shutdown();
  999. }
  1000. machine_kexec(kexec_image);
  1001. #ifdef CONFIG_KEXEC_JUMP
  1002. if (kexec_image->preserve_context) {
  1003. syscore_resume();
  1004. Enable_irqs:
  1005. local_irq_enable();
  1006. Enable_cpus:
  1007. enable_nonboot_cpus();
  1008. dpm_resume_start(PMSG_RESTORE);
  1009. Resume_devices:
  1010. dpm_resume_end(PMSG_RESTORE);
  1011. Resume_console:
  1012. resume_console();
  1013. thaw_processes();
  1014. Restore_console:
  1015. pm_restore_console();
  1016. unlock_system_sleep();
  1017. }
  1018. #endif
  1019. Unlock:
  1020. mutex_unlock(&kexec_mutex);
  1021. return error;
  1022. }
  1023. /*
  1024. * Protection mechanism for crashkernel reserved memory after
  1025. * the kdump kernel is loaded.
  1026. *
  1027. * Provide an empty default implementation here -- architecture
  1028. * code may override this
  1029. */
  1030. void __weak arch_kexec_protect_crashkres(void)
  1031. {}
  1032. void __weak arch_kexec_unprotect_crashkres(void)
  1033. {}