kexec_core.c 31 KB

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