kexec_core.c 31 KB

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