kexec_core.c 31 KB

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