kexec_core.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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 <asm/page.h>
  40. #include <asm/sections.h>
  41. #include <crypto/hash.h>
  42. #include <crypto/sha.h>
  43. #include "kexec_internal.h"
  44. DEFINE_MUTEX(kexec_mutex);
  45. /* Per cpu memory for storing cpu states in case of system crash. */
  46. note_buf_t __percpu *crash_notes;
  47. /* vmcoreinfo stuff */
  48. static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
  49. u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
  50. size_t vmcoreinfo_size;
  51. size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
  52. /* Flag to indicate we are going to kexec a new kernel */
  53. bool kexec_in_progress = false;
  54. /* Location of the reserved area for the crash kernel */
  55. struct resource crashk_res = {
  56. .name = "Crash kernel",
  57. .start = 0,
  58. .end = 0,
  59. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  60. };
  61. struct resource crashk_low_res = {
  62. .name = "Crash kernel",
  63. .start = 0,
  64. .end = 0,
  65. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  66. };
  67. int kexec_should_crash(struct task_struct *p)
  68. {
  69. /*
  70. * If crash_kexec_post_notifiers is enabled, don't run
  71. * crash_kexec() here yet, which must be run after panic
  72. * notifiers in panic().
  73. */
  74. if (crash_kexec_post_notifiers)
  75. return 0;
  76. /*
  77. * There are 4 panic() calls in do_exit() path, each of which
  78. * corresponds to each of these 4 conditions.
  79. */
  80. if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
  81. return 1;
  82. return 0;
  83. }
  84. /*
  85. * When kexec transitions to the new kernel there is a one-to-one
  86. * mapping between physical and virtual addresses. On processors
  87. * where you can disable the MMU this is trivial, and easy. For
  88. * others it is still a simple predictable page table to setup.
  89. *
  90. * In that environment kexec copies the new kernel to its final
  91. * resting place. This means I can only support memory whose
  92. * physical address can fit in an unsigned long. In particular
  93. * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
  94. * If the assembly stub has more restrictive requirements
  95. * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
  96. * defined more restrictively in <asm/kexec.h>.
  97. *
  98. * The code for the transition from the current kernel to the
  99. * the new kernel is placed in the control_code_buffer, whose size
  100. * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
  101. * page of memory is necessary, but some architectures require more.
  102. * Because this memory must be identity mapped in the transition from
  103. * virtual to physical addresses it must live in the range
  104. * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
  105. * modifiable.
  106. *
  107. * The assembly stub in the control code buffer is passed a linked list
  108. * of descriptor pages detailing the source pages of the new kernel,
  109. * and the destination addresses of those source pages. As this data
  110. * structure is not used in the context of the current OS, it must
  111. * be self-contained.
  112. *
  113. * The code has been made to work with highmem pages and will use a
  114. * destination page in its final resting place (if it happens
  115. * to allocate it). The end product of this is that most of the
  116. * physical address space, and most of RAM can be used.
  117. *
  118. * Future directions include:
  119. * - allocating a page table with the control code buffer identity
  120. * mapped, to simplify machine_kexec and make kexec_on_panic more
  121. * reliable.
  122. */
  123. /*
  124. * KIMAGE_NO_DEST is an impossible destination address..., for
  125. * allocating pages whose destination address we do not care about.
  126. */
  127. #define KIMAGE_NO_DEST (-1UL)
  128. static struct page *kimage_alloc_page(struct kimage *image,
  129. gfp_t gfp_mask,
  130. unsigned long dest);
  131. int sanity_check_segment_list(struct kimage *image)
  132. {
  133. int result, i;
  134. unsigned long nr_segments = image->nr_segments;
  135. /*
  136. * Verify we have good destination addresses. The caller is
  137. * responsible for making certain we don't attempt to load
  138. * the new image into invalid or reserved areas of RAM. This
  139. * just verifies it is an address we can use.
  140. *
  141. * Since the kernel does everything in page size chunks ensure
  142. * the destination addresses are page aligned. Too many
  143. * special cases crop of when we don't do this. The most
  144. * insidious is getting overlapping destination addresses
  145. * simply because addresses are changed to page size
  146. * granularity.
  147. */
  148. result = -EADDRNOTAVAIL;
  149. for (i = 0; i < nr_segments; i++) {
  150. unsigned long mstart, mend;
  151. mstart = image->segment[i].mem;
  152. mend = mstart + image->segment[i].memsz;
  153. if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
  154. return result;
  155. if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
  156. return result;
  157. }
  158. /* Verify our destination addresses do not overlap.
  159. * If we alloed overlapping destination addresses
  160. * through very weird things can happen with no
  161. * easy explanation as one segment stops on another.
  162. */
  163. result = -EINVAL;
  164. for (i = 0; i < nr_segments; i++) {
  165. unsigned long mstart, mend;
  166. unsigned long j;
  167. mstart = image->segment[i].mem;
  168. mend = mstart + image->segment[i].memsz;
  169. for (j = 0; j < i; j++) {
  170. unsigned long pstart, pend;
  171. pstart = image->segment[j].mem;
  172. pend = pstart + image->segment[j].memsz;
  173. /* Do the segments overlap ? */
  174. if ((mend > pstart) && (mstart < pend))
  175. return result;
  176. }
  177. }
  178. /* Ensure our buffer sizes are strictly less than
  179. * our memory sizes. This should always be the case,
  180. * and it is easier to check up front than to be surprised
  181. * later on.
  182. */
  183. result = -EINVAL;
  184. for (i = 0; i < nr_segments; i++) {
  185. if (image->segment[i].bufsz > image->segment[i].memsz)
  186. return result;
  187. }
  188. /*
  189. * Verify we have good destination addresses. Normally
  190. * the caller is responsible for making certain we don't
  191. * attempt to load the new image into invalid or reserved
  192. * areas of RAM. But crash kernels are preloaded into a
  193. * reserved area of ram. We must ensure the addresses
  194. * are in the reserved area otherwise preloading the
  195. * kernel could corrupt things.
  196. */
  197. if (image->type == KEXEC_TYPE_CRASH) {
  198. result = -EADDRNOTAVAIL;
  199. for (i = 0; i < nr_segments; i++) {
  200. unsigned long mstart, mend;
  201. mstart = image->segment[i].mem;
  202. mend = mstart + image->segment[i].memsz - 1;
  203. /* Ensure we are within the crash kernel limits */
  204. if ((mstart < crashk_res.start) ||
  205. (mend > crashk_res.end))
  206. return result;
  207. }
  208. }
  209. return 0;
  210. }
  211. struct kimage *do_kimage_alloc_init(void)
  212. {
  213. struct kimage *image;
  214. /* Allocate a controlling structure */
  215. image = kzalloc(sizeof(*image), GFP_KERNEL);
  216. if (!image)
  217. return NULL;
  218. image->head = 0;
  219. image->entry = &image->head;
  220. image->last_entry = &image->head;
  221. image->control_page = ~0; /* By default this does not apply */
  222. image->type = KEXEC_TYPE_DEFAULT;
  223. /* Initialize the list of control pages */
  224. INIT_LIST_HEAD(&image->control_pages);
  225. /* Initialize the list of destination pages */
  226. INIT_LIST_HEAD(&image->dest_pages);
  227. /* Initialize the list of unusable pages */
  228. INIT_LIST_HEAD(&image->unusable_pages);
  229. return image;
  230. }
  231. int kimage_is_destination_range(struct kimage *image,
  232. unsigned long start,
  233. unsigned long end)
  234. {
  235. unsigned long i;
  236. for (i = 0; i < image->nr_segments; i++) {
  237. unsigned long mstart, mend;
  238. mstart = image->segment[i].mem;
  239. mend = mstart + image->segment[i].memsz;
  240. if ((end > mstart) && (start < mend))
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
  246. {
  247. struct page *pages;
  248. pages = alloc_pages(gfp_mask, order);
  249. if (pages) {
  250. unsigned int count, i;
  251. pages->mapping = NULL;
  252. set_page_private(pages, order);
  253. count = 1 << order;
  254. for (i = 0; i < count; i++)
  255. SetPageReserved(pages + i);
  256. }
  257. return pages;
  258. }
  259. static void kimage_free_pages(struct page *page)
  260. {
  261. unsigned int order, count, i;
  262. order = page_private(page);
  263. count = 1 << order;
  264. for (i = 0; i < count; i++)
  265. ClearPageReserved(page + i);
  266. __free_pages(page, order);
  267. }
  268. void kimage_free_page_list(struct list_head *list)
  269. {
  270. struct list_head *pos, *next;
  271. list_for_each_safe(pos, next, list) {
  272. struct page *page;
  273. page = list_entry(pos, struct page, lru);
  274. list_del(&page->lru);
  275. kimage_free_pages(page);
  276. }
  277. }
  278. static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
  279. unsigned int order)
  280. {
  281. /* Control pages are special, they are the intermediaries
  282. * that are needed while we copy the rest of the pages
  283. * to their final resting place. As such they must
  284. * not conflict with either the destination addresses
  285. * or memory the kernel is already using.
  286. *
  287. * The only case where we really need more than one of
  288. * these are for architectures where we cannot disable
  289. * the MMU and must instead generate an identity mapped
  290. * page table for all of the memory.
  291. *
  292. * At worst this runs in O(N) of the image size.
  293. */
  294. struct list_head extra_pages;
  295. struct page *pages;
  296. unsigned int count;
  297. count = 1 << order;
  298. INIT_LIST_HEAD(&extra_pages);
  299. /* Loop while I can allocate a page and the page allocated
  300. * is a destination page.
  301. */
  302. do {
  303. unsigned long pfn, epfn, addr, eaddr;
  304. pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);
  305. if (!pages)
  306. break;
  307. pfn = page_to_pfn(pages);
  308. epfn = pfn + count;
  309. addr = pfn << PAGE_SHIFT;
  310. eaddr = epfn << PAGE_SHIFT;
  311. if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
  312. kimage_is_destination_range(image, addr, eaddr)) {
  313. list_add(&pages->lru, &extra_pages);
  314. pages = NULL;
  315. }
  316. } while (!pages);
  317. if (pages) {
  318. /* Remember the allocated page... */
  319. list_add(&pages->lru, &image->control_pages);
  320. /* Because the page is already in it's destination
  321. * location we will never allocate another page at
  322. * that address. Therefore kimage_alloc_pages
  323. * will not return it (again) and we don't need
  324. * to give it an entry in image->segment[].
  325. */
  326. }
  327. /* Deal with the destination pages I have inadvertently allocated.
  328. *
  329. * Ideally I would convert multi-page allocations into single
  330. * page allocations, and add everything to image->dest_pages.
  331. *
  332. * For now it is simpler to just free the pages.
  333. */
  334. kimage_free_page_list(&extra_pages);
  335. return pages;
  336. }
  337. static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
  338. unsigned int order)
  339. {
  340. /* Control pages are special, they are the intermediaries
  341. * that are needed while we copy the rest of the pages
  342. * to their final resting place. As such they must
  343. * not conflict with either the destination addresses
  344. * or memory the kernel is already using.
  345. *
  346. * Control pages are also the only pags we must allocate
  347. * when loading a crash kernel. All of the other pages
  348. * are specified by the segments and we just memcpy
  349. * into them directly.
  350. *
  351. * The only case where we really need more than one of
  352. * these are for architectures where we cannot disable
  353. * the MMU and must instead generate an identity mapped
  354. * page table for all of the memory.
  355. *
  356. * Given the low demand this implements a very simple
  357. * allocator that finds the first hole of the appropriate
  358. * size in the reserved memory region, and allocates all
  359. * of the memory up to and including the hole.
  360. */
  361. unsigned long hole_start, hole_end, size;
  362. struct page *pages;
  363. pages = NULL;
  364. size = (1 << order) << PAGE_SHIFT;
  365. hole_start = (image->control_page + (size - 1)) & ~(size - 1);
  366. hole_end = hole_start + size - 1;
  367. while (hole_end <= crashk_res.end) {
  368. unsigned long i;
  369. if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
  370. break;
  371. /* See if I overlap any of the segments */
  372. for (i = 0; i < image->nr_segments; i++) {
  373. unsigned long mstart, mend;
  374. mstart = image->segment[i].mem;
  375. mend = mstart + image->segment[i].memsz - 1;
  376. if ((hole_end >= mstart) && (hole_start <= mend)) {
  377. /* Advance the hole to the end of the segment */
  378. hole_start = (mend + (size - 1)) & ~(size - 1);
  379. hole_end = hole_start + size - 1;
  380. break;
  381. }
  382. }
  383. /* If I don't overlap any segments I have found my hole! */
  384. if (i == image->nr_segments) {
  385. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  386. image->control_page = hole_end;
  387. break;
  388. }
  389. }
  390. return pages;
  391. }
  392. struct page *kimage_alloc_control_pages(struct kimage *image,
  393. unsigned int order)
  394. {
  395. struct page *pages = NULL;
  396. switch (image->type) {
  397. case KEXEC_TYPE_DEFAULT:
  398. pages = kimage_alloc_normal_control_pages(image, order);
  399. break;
  400. case KEXEC_TYPE_CRASH:
  401. pages = kimage_alloc_crash_control_pages(image, order);
  402. break;
  403. }
  404. return pages;
  405. }
  406. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  407. {
  408. if (*image->entry != 0)
  409. image->entry++;
  410. if (image->entry == image->last_entry) {
  411. kimage_entry_t *ind_page;
  412. struct page *page;
  413. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  414. if (!page)
  415. return -ENOMEM;
  416. ind_page = page_address(page);
  417. *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
  418. image->entry = ind_page;
  419. image->last_entry = ind_page +
  420. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  421. }
  422. *image->entry = entry;
  423. image->entry++;
  424. *image->entry = 0;
  425. return 0;
  426. }
  427. static int kimage_set_destination(struct kimage *image,
  428. unsigned long destination)
  429. {
  430. int result;
  431. destination &= PAGE_MASK;
  432. result = kimage_add_entry(image, destination | IND_DESTINATION);
  433. return result;
  434. }
  435. static int kimage_add_page(struct kimage *image, unsigned long page)
  436. {
  437. int result;
  438. page &= PAGE_MASK;
  439. result = kimage_add_entry(image, page | IND_SOURCE);
  440. return result;
  441. }
  442. static void kimage_free_extra_pages(struct kimage *image)
  443. {
  444. /* Walk through and free any extra destination pages I may have */
  445. kimage_free_page_list(&image->dest_pages);
  446. /* Walk through and free any unusable pages I have cached */
  447. kimage_free_page_list(&image->unusable_pages);
  448. }
  449. void kimage_terminate(struct kimage *image)
  450. {
  451. if (*image->entry != 0)
  452. image->entry++;
  453. *image->entry = IND_DONE;
  454. }
  455. #define for_each_kimage_entry(image, ptr, entry) \
  456. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  457. ptr = (entry & IND_INDIRECTION) ? \
  458. phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  459. static void kimage_free_entry(kimage_entry_t entry)
  460. {
  461. struct page *page;
  462. page = pfn_to_page(entry >> PAGE_SHIFT);
  463. kimage_free_pages(page);
  464. }
  465. void kimage_free(struct kimage *image)
  466. {
  467. kimage_entry_t *ptr, entry;
  468. kimage_entry_t ind = 0;
  469. if (!image)
  470. return;
  471. kimage_free_extra_pages(image);
  472. for_each_kimage_entry(image, ptr, entry) {
  473. if (entry & IND_INDIRECTION) {
  474. /* Free the previous indirection page */
  475. if (ind & IND_INDIRECTION)
  476. kimage_free_entry(ind);
  477. /* Save this indirection page until we are
  478. * done with it.
  479. */
  480. ind = entry;
  481. } else if (entry & IND_SOURCE)
  482. kimage_free_entry(entry);
  483. }
  484. /* Free the final indirection page */
  485. if (ind & IND_INDIRECTION)
  486. kimage_free_entry(ind);
  487. /* Handle any machine specific cleanup */
  488. machine_kexec_cleanup(image);
  489. /* Free the kexec control pages... */
  490. kimage_free_page_list(&image->control_pages);
  491. /*
  492. * Free up any temporary buffers allocated. This might hit if
  493. * error occurred much later after buffer allocation.
  494. */
  495. if (image->file_mode)
  496. kimage_file_post_load_cleanup(image);
  497. kfree(image);
  498. }
  499. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  500. unsigned long page)
  501. {
  502. kimage_entry_t *ptr, entry;
  503. unsigned long destination = 0;
  504. for_each_kimage_entry(image, ptr, entry) {
  505. if (entry & IND_DESTINATION)
  506. destination = entry & PAGE_MASK;
  507. else if (entry & IND_SOURCE) {
  508. if (page == destination)
  509. return ptr;
  510. destination += PAGE_SIZE;
  511. }
  512. }
  513. return NULL;
  514. }
  515. static struct page *kimage_alloc_page(struct kimage *image,
  516. gfp_t gfp_mask,
  517. unsigned long destination)
  518. {
  519. /*
  520. * Here we implement safeguards to ensure that a source page
  521. * is not copied to its destination page before the data on
  522. * the destination page is no longer useful.
  523. *
  524. * To do this we maintain the invariant that a source page is
  525. * either its own destination page, or it is not a
  526. * destination page at all.
  527. *
  528. * That is slightly stronger than required, but the proof
  529. * that no problems will not occur is trivial, and the
  530. * implementation is simply to verify.
  531. *
  532. * When allocating all pages normally this algorithm will run
  533. * in O(N) time, but in the worst case it will run in O(N^2)
  534. * time. If the runtime is a problem the data structures can
  535. * be fixed.
  536. */
  537. struct page *page;
  538. unsigned long addr;
  539. /*
  540. * Walk through the list of destination pages, and see if I
  541. * have a match.
  542. */
  543. list_for_each_entry(page, &image->dest_pages, lru) {
  544. addr = page_to_pfn(page) << PAGE_SHIFT;
  545. if (addr == destination) {
  546. list_del(&page->lru);
  547. return page;
  548. }
  549. }
  550. page = NULL;
  551. while (1) {
  552. kimage_entry_t *old;
  553. /* Allocate a page, if we run out of memory give up */
  554. page = kimage_alloc_pages(gfp_mask, 0);
  555. if (!page)
  556. return NULL;
  557. /* If the page cannot be used file it away */
  558. if (page_to_pfn(page) >
  559. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  560. list_add(&page->lru, &image->unusable_pages);
  561. continue;
  562. }
  563. addr = page_to_pfn(page) << PAGE_SHIFT;
  564. /* If it is the destination page we want use it */
  565. if (addr == destination)
  566. break;
  567. /* If the page is not a destination page use it */
  568. if (!kimage_is_destination_range(image, addr,
  569. addr + PAGE_SIZE))
  570. break;
  571. /*
  572. * I know that the page is someones destination page.
  573. * See if there is already a source page for this
  574. * destination page. And if so swap the source pages.
  575. */
  576. old = kimage_dst_used(image, addr);
  577. if (old) {
  578. /* If so move it */
  579. unsigned long old_addr;
  580. struct page *old_page;
  581. old_addr = *old & PAGE_MASK;
  582. old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
  583. copy_highpage(page, old_page);
  584. *old = addr | (*old & ~PAGE_MASK);
  585. /* The old page I have found cannot be a
  586. * destination page, so return it if it's
  587. * gfp_flags honor the ones passed in.
  588. */
  589. if (!(gfp_mask & __GFP_HIGHMEM) &&
  590. PageHighMem(old_page)) {
  591. kimage_free_pages(old_page);
  592. continue;
  593. }
  594. addr = old_addr;
  595. page = old_page;
  596. break;
  597. }
  598. /* Place the page on the destination list, to be used later */
  599. list_add(&page->lru, &image->dest_pages);
  600. }
  601. return page;
  602. }
  603. static int kimage_load_normal_segment(struct kimage *image,
  604. struct kexec_segment *segment)
  605. {
  606. unsigned long maddr;
  607. size_t ubytes, mbytes;
  608. int result;
  609. unsigned char __user *buf = NULL;
  610. unsigned char *kbuf = NULL;
  611. result = 0;
  612. if (image->file_mode)
  613. kbuf = segment->kbuf;
  614. else
  615. buf = segment->buf;
  616. ubytes = segment->bufsz;
  617. mbytes = segment->memsz;
  618. maddr = segment->mem;
  619. result = kimage_set_destination(image, maddr);
  620. if (result < 0)
  621. goto out;
  622. while (mbytes) {
  623. struct page *page;
  624. char *ptr;
  625. size_t uchunk, mchunk;
  626. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  627. if (!page) {
  628. result = -ENOMEM;
  629. goto out;
  630. }
  631. result = kimage_add_page(image, page_to_pfn(page)
  632. << PAGE_SHIFT);
  633. if (result < 0)
  634. goto out;
  635. ptr = kmap(page);
  636. /* Start with a clear page */
  637. clear_page(ptr);
  638. ptr += maddr & ~PAGE_MASK;
  639. mchunk = min_t(size_t, mbytes,
  640. PAGE_SIZE - (maddr & ~PAGE_MASK));
  641. uchunk = min(ubytes, mchunk);
  642. /* For file based kexec, source pages are in kernel memory */
  643. if (image->file_mode)
  644. memcpy(ptr, kbuf, uchunk);
  645. else
  646. result = copy_from_user(ptr, buf, uchunk);
  647. kunmap(page);
  648. if (result) {
  649. result = -EFAULT;
  650. goto out;
  651. }
  652. ubytes -= uchunk;
  653. maddr += mchunk;
  654. if (image->file_mode)
  655. kbuf += mchunk;
  656. else
  657. buf += mchunk;
  658. mbytes -= mchunk;
  659. }
  660. out:
  661. return result;
  662. }
  663. static int kimage_load_crash_segment(struct kimage *image,
  664. struct kexec_segment *segment)
  665. {
  666. /* For crash dumps kernels we simply copy the data from
  667. * user space to it's destination.
  668. * We do things a page at a time for the sake of kmap.
  669. */
  670. unsigned long maddr;
  671. size_t ubytes, mbytes;
  672. int result;
  673. unsigned char __user *buf = NULL;
  674. unsigned char *kbuf = NULL;
  675. result = 0;
  676. if (image->file_mode)
  677. kbuf = segment->kbuf;
  678. else
  679. buf = segment->buf;
  680. ubytes = segment->bufsz;
  681. mbytes = segment->memsz;
  682. maddr = segment->mem;
  683. while (mbytes) {
  684. struct page *page;
  685. char *ptr;
  686. size_t uchunk, mchunk;
  687. page = pfn_to_page(maddr >> PAGE_SHIFT);
  688. if (!page) {
  689. result = -ENOMEM;
  690. goto out;
  691. }
  692. ptr = kmap(page);
  693. ptr += maddr & ~PAGE_MASK;
  694. mchunk = min_t(size_t, mbytes,
  695. PAGE_SIZE - (maddr & ~PAGE_MASK));
  696. uchunk = min(ubytes, mchunk);
  697. if (mchunk > uchunk) {
  698. /* Zero the trailing part of the page */
  699. memset(ptr + uchunk, 0, mchunk - uchunk);
  700. }
  701. /* For file based kexec, source pages are in kernel memory */
  702. if (image->file_mode)
  703. memcpy(ptr, kbuf, uchunk);
  704. else
  705. result = copy_from_user(ptr, buf, uchunk);
  706. kexec_flush_icache_page(page);
  707. kunmap(page);
  708. if (result) {
  709. result = -EFAULT;
  710. goto out;
  711. }
  712. ubytes -= uchunk;
  713. maddr += mchunk;
  714. if (image->file_mode)
  715. kbuf += mchunk;
  716. else
  717. buf += mchunk;
  718. mbytes -= mchunk;
  719. }
  720. out:
  721. return result;
  722. }
  723. int kimage_load_segment(struct kimage *image,
  724. struct kexec_segment *segment)
  725. {
  726. int result = -ENOMEM;
  727. switch (image->type) {
  728. case KEXEC_TYPE_DEFAULT:
  729. result = kimage_load_normal_segment(image, segment);
  730. break;
  731. case KEXEC_TYPE_CRASH:
  732. result = kimage_load_crash_segment(image, segment);
  733. break;
  734. }
  735. return result;
  736. }
  737. struct kimage *kexec_image;
  738. struct kimage *kexec_crash_image;
  739. int kexec_load_disabled;
  740. void crash_kexec(struct pt_regs *regs)
  741. {
  742. /* Take the kexec_mutex here to prevent sys_kexec_load
  743. * running on one cpu from replacing the crash kernel
  744. * we are using after a panic on a different cpu.
  745. *
  746. * If the crash kernel was not located in a fixed area
  747. * of memory the xchg(&kexec_crash_image) would be
  748. * sufficient. But since I reuse the memory...
  749. */
  750. if (mutex_trylock(&kexec_mutex)) {
  751. if (kexec_crash_image) {
  752. struct pt_regs fixed_regs;
  753. crash_setup_regs(&fixed_regs, regs);
  754. crash_save_vmcoreinfo();
  755. machine_crash_shutdown(&fixed_regs);
  756. machine_kexec(kexec_crash_image);
  757. }
  758. mutex_unlock(&kexec_mutex);
  759. }
  760. }
  761. size_t crash_get_memory_size(void)
  762. {
  763. size_t size = 0;
  764. mutex_lock(&kexec_mutex);
  765. if (crashk_res.end != crashk_res.start)
  766. size = resource_size(&crashk_res);
  767. mutex_unlock(&kexec_mutex);
  768. return size;
  769. }
  770. void __weak crash_free_reserved_phys_range(unsigned long begin,
  771. unsigned long end)
  772. {
  773. unsigned long addr;
  774. for (addr = begin; addr < end; addr += PAGE_SIZE)
  775. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  776. }
  777. int crash_shrink_memory(unsigned long new_size)
  778. {
  779. int ret = 0;
  780. unsigned long start, end;
  781. unsigned long old_size;
  782. struct resource *ram_res;
  783. mutex_lock(&kexec_mutex);
  784. if (kexec_crash_image) {
  785. ret = -ENOENT;
  786. goto unlock;
  787. }
  788. start = crashk_res.start;
  789. end = crashk_res.end;
  790. old_size = (end == 0) ? 0 : end - start + 1;
  791. if (new_size >= old_size) {
  792. ret = (new_size == old_size) ? 0 : -EINVAL;
  793. goto unlock;
  794. }
  795. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  796. if (!ram_res) {
  797. ret = -ENOMEM;
  798. goto unlock;
  799. }
  800. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  801. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  802. crash_map_reserved_pages();
  803. crash_free_reserved_phys_range(end, crashk_res.end);
  804. if ((start == end) && (crashk_res.parent != NULL))
  805. release_resource(&crashk_res);
  806. ram_res->start = end;
  807. ram_res->end = crashk_res.end;
  808. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  809. ram_res->name = "System RAM";
  810. crashk_res.end = end - 1;
  811. insert_resource(&iomem_resource, ram_res);
  812. crash_unmap_reserved_pages();
  813. unlock:
  814. mutex_unlock(&kexec_mutex);
  815. return ret;
  816. }
  817. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  818. size_t data_len)
  819. {
  820. struct elf_note note;
  821. note.n_namesz = strlen(name) + 1;
  822. note.n_descsz = data_len;
  823. note.n_type = type;
  824. memcpy(buf, &note, sizeof(note));
  825. buf += (sizeof(note) + 3)/4;
  826. memcpy(buf, name, note.n_namesz);
  827. buf += (note.n_namesz + 3)/4;
  828. memcpy(buf, data, note.n_descsz);
  829. buf += (note.n_descsz + 3)/4;
  830. return buf;
  831. }
  832. static void final_note(u32 *buf)
  833. {
  834. struct elf_note note;
  835. note.n_namesz = 0;
  836. note.n_descsz = 0;
  837. note.n_type = 0;
  838. memcpy(buf, &note, sizeof(note));
  839. }
  840. void crash_save_cpu(struct pt_regs *regs, int cpu)
  841. {
  842. struct elf_prstatus prstatus;
  843. u32 *buf;
  844. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  845. return;
  846. /* Using ELF notes here is opportunistic.
  847. * I need a well defined structure format
  848. * for the data I pass, and I need tags
  849. * on the data to indicate what information I have
  850. * squirrelled away. ELF notes happen to provide
  851. * all of that, so there is no need to invent something new.
  852. */
  853. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  854. if (!buf)
  855. return;
  856. memset(&prstatus, 0, sizeof(prstatus));
  857. prstatus.pr_pid = current->pid;
  858. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  859. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  860. &prstatus, sizeof(prstatus));
  861. final_note(buf);
  862. }
  863. static int __init crash_notes_memory_init(void)
  864. {
  865. /* Allocate memory for saving cpu registers. */
  866. size_t size, align;
  867. /*
  868. * crash_notes could be allocated across 2 vmalloc pages when percpu
  869. * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
  870. * pages are also on 2 continuous physical pages. In this case the
  871. * 2nd part of crash_notes in 2nd page could be lost since only the
  872. * starting address and size of crash_notes are exported through sysfs.
  873. * Here round up the size of crash_notes to the nearest power of two
  874. * and pass it to __alloc_percpu as align value. This can make sure
  875. * crash_notes is allocated inside one physical page.
  876. */
  877. size = sizeof(note_buf_t);
  878. align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
  879. /*
  880. * Break compile if size is bigger than PAGE_SIZE since crash_notes
  881. * definitely will be in 2 pages with that.
  882. */
  883. BUILD_BUG_ON(size > PAGE_SIZE);
  884. crash_notes = __alloc_percpu(size, align);
  885. if (!crash_notes) {
  886. pr_warn("Memory allocation for saving cpu register states failed\n");
  887. return -ENOMEM;
  888. }
  889. return 0;
  890. }
  891. subsys_initcall(crash_notes_memory_init);
  892. /*
  893. * parsing the "crashkernel" commandline
  894. *
  895. * this code is intended to be called from architecture specific code
  896. */
  897. /*
  898. * This function parses command lines in the format
  899. *
  900. * crashkernel=ramsize-range:size[,...][@offset]
  901. *
  902. * The function returns 0 on success and -EINVAL on failure.
  903. */
  904. static int __init parse_crashkernel_mem(char *cmdline,
  905. unsigned long long system_ram,
  906. unsigned long long *crash_size,
  907. unsigned long long *crash_base)
  908. {
  909. char *cur = cmdline, *tmp;
  910. /* for each entry of the comma-separated list */
  911. do {
  912. unsigned long long start, end = ULLONG_MAX, size;
  913. /* get the start of the range */
  914. start = memparse(cur, &tmp);
  915. if (cur == tmp) {
  916. pr_warn("crashkernel: Memory value expected\n");
  917. return -EINVAL;
  918. }
  919. cur = tmp;
  920. if (*cur != '-') {
  921. pr_warn("crashkernel: '-' expected\n");
  922. return -EINVAL;
  923. }
  924. cur++;
  925. /* if no ':' is here, than we read the end */
  926. if (*cur != ':') {
  927. end = memparse(cur, &tmp);
  928. if (cur == tmp) {
  929. pr_warn("crashkernel: Memory value expected\n");
  930. return -EINVAL;
  931. }
  932. cur = tmp;
  933. if (end <= start) {
  934. pr_warn("crashkernel: end <= start\n");
  935. return -EINVAL;
  936. }
  937. }
  938. if (*cur != ':') {
  939. pr_warn("crashkernel: ':' expected\n");
  940. return -EINVAL;
  941. }
  942. cur++;
  943. size = memparse(cur, &tmp);
  944. if (cur == tmp) {
  945. pr_warn("Memory value expected\n");
  946. return -EINVAL;
  947. }
  948. cur = tmp;
  949. if (size >= system_ram) {
  950. pr_warn("crashkernel: invalid size\n");
  951. return -EINVAL;
  952. }
  953. /* match ? */
  954. if (system_ram >= start && system_ram < end) {
  955. *crash_size = size;
  956. break;
  957. }
  958. } while (*cur++ == ',');
  959. if (*crash_size > 0) {
  960. while (*cur && *cur != ' ' && *cur != '@')
  961. cur++;
  962. if (*cur == '@') {
  963. cur++;
  964. *crash_base = memparse(cur, &tmp);
  965. if (cur == tmp) {
  966. pr_warn("Memory value expected after '@'\n");
  967. return -EINVAL;
  968. }
  969. }
  970. }
  971. return 0;
  972. }
  973. /*
  974. * That function parses "simple" (old) crashkernel command lines like
  975. *
  976. * crashkernel=size[@offset]
  977. *
  978. * It returns 0 on success and -EINVAL on failure.
  979. */
  980. static int __init parse_crashkernel_simple(char *cmdline,
  981. unsigned long long *crash_size,
  982. unsigned long long *crash_base)
  983. {
  984. char *cur = cmdline;
  985. *crash_size = memparse(cmdline, &cur);
  986. if (cmdline == cur) {
  987. pr_warn("crashkernel: memory value expected\n");
  988. return -EINVAL;
  989. }
  990. if (*cur == '@')
  991. *crash_base = memparse(cur+1, &cur);
  992. else if (*cur != ' ' && *cur != '\0') {
  993. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  994. return -EINVAL;
  995. }
  996. return 0;
  997. }
  998. #define SUFFIX_HIGH 0
  999. #define SUFFIX_LOW 1
  1000. #define SUFFIX_NULL 2
  1001. static __initdata char *suffix_tbl[] = {
  1002. [SUFFIX_HIGH] = ",high",
  1003. [SUFFIX_LOW] = ",low",
  1004. [SUFFIX_NULL] = NULL,
  1005. };
  1006. /*
  1007. * That function parses "suffix" crashkernel command lines like
  1008. *
  1009. * crashkernel=size,[high|low]
  1010. *
  1011. * It returns 0 on success and -EINVAL on failure.
  1012. */
  1013. static int __init parse_crashkernel_suffix(char *cmdline,
  1014. unsigned long long *crash_size,
  1015. const char *suffix)
  1016. {
  1017. char *cur = cmdline;
  1018. *crash_size = memparse(cmdline, &cur);
  1019. if (cmdline == cur) {
  1020. pr_warn("crashkernel: memory value expected\n");
  1021. return -EINVAL;
  1022. }
  1023. /* check with suffix */
  1024. if (strncmp(cur, suffix, strlen(suffix))) {
  1025. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1026. return -EINVAL;
  1027. }
  1028. cur += strlen(suffix);
  1029. if (*cur != ' ' && *cur != '\0') {
  1030. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1031. return -EINVAL;
  1032. }
  1033. return 0;
  1034. }
  1035. static __init char *get_last_crashkernel(char *cmdline,
  1036. const char *name,
  1037. const char *suffix)
  1038. {
  1039. char *p = cmdline, *ck_cmdline = NULL;
  1040. /* find crashkernel and use the last one if there are more */
  1041. p = strstr(p, name);
  1042. while (p) {
  1043. char *end_p = strchr(p, ' ');
  1044. char *q;
  1045. if (!end_p)
  1046. end_p = p + strlen(p);
  1047. if (!suffix) {
  1048. int i;
  1049. /* skip the one with any known suffix */
  1050. for (i = 0; suffix_tbl[i]; i++) {
  1051. q = end_p - strlen(suffix_tbl[i]);
  1052. if (!strncmp(q, suffix_tbl[i],
  1053. strlen(suffix_tbl[i])))
  1054. goto next;
  1055. }
  1056. ck_cmdline = p;
  1057. } else {
  1058. q = end_p - strlen(suffix);
  1059. if (!strncmp(q, suffix, strlen(suffix)))
  1060. ck_cmdline = p;
  1061. }
  1062. next:
  1063. p = strstr(p+1, name);
  1064. }
  1065. if (!ck_cmdline)
  1066. return NULL;
  1067. return ck_cmdline;
  1068. }
  1069. static int __init __parse_crashkernel(char *cmdline,
  1070. unsigned long long system_ram,
  1071. unsigned long long *crash_size,
  1072. unsigned long long *crash_base,
  1073. const char *name,
  1074. const char *suffix)
  1075. {
  1076. char *first_colon, *first_space;
  1077. char *ck_cmdline;
  1078. BUG_ON(!crash_size || !crash_base);
  1079. *crash_size = 0;
  1080. *crash_base = 0;
  1081. ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
  1082. if (!ck_cmdline)
  1083. return -EINVAL;
  1084. ck_cmdline += strlen(name);
  1085. if (suffix)
  1086. return parse_crashkernel_suffix(ck_cmdline, crash_size,
  1087. suffix);
  1088. /*
  1089. * if the commandline contains a ':', then that's the extended
  1090. * syntax -- if not, it must be the classic syntax
  1091. */
  1092. first_colon = strchr(ck_cmdline, ':');
  1093. first_space = strchr(ck_cmdline, ' ');
  1094. if (first_colon && (!first_space || first_colon < first_space))
  1095. return parse_crashkernel_mem(ck_cmdline, system_ram,
  1096. crash_size, crash_base);
  1097. return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
  1098. }
  1099. /*
  1100. * That function is the entry point for command line parsing and should be
  1101. * called from the arch-specific code.
  1102. */
  1103. int __init parse_crashkernel(char *cmdline,
  1104. unsigned long long system_ram,
  1105. unsigned long long *crash_size,
  1106. unsigned long long *crash_base)
  1107. {
  1108. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1109. "crashkernel=", NULL);
  1110. }
  1111. int __init parse_crashkernel_high(char *cmdline,
  1112. unsigned long long system_ram,
  1113. unsigned long long *crash_size,
  1114. unsigned long long *crash_base)
  1115. {
  1116. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1117. "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
  1118. }
  1119. int __init parse_crashkernel_low(char *cmdline,
  1120. unsigned long long system_ram,
  1121. unsigned long long *crash_size,
  1122. unsigned long long *crash_base)
  1123. {
  1124. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1125. "crashkernel=", suffix_tbl[SUFFIX_LOW]);
  1126. }
  1127. static void update_vmcoreinfo_note(void)
  1128. {
  1129. u32 *buf = vmcoreinfo_note;
  1130. if (!vmcoreinfo_size)
  1131. return;
  1132. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  1133. vmcoreinfo_size);
  1134. final_note(buf);
  1135. }
  1136. void crash_save_vmcoreinfo(void)
  1137. {
  1138. vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
  1139. update_vmcoreinfo_note();
  1140. }
  1141. void vmcoreinfo_append_str(const char *fmt, ...)
  1142. {
  1143. va_list args;
  1144. char buf[0x50];
  1145. size_t r;
  1146. va_start(args, fmt);
  1147. r = vscnprintf(buf, sizeof(buf), fmt, args);
  1148. va_end(args);
  1149. r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
  1150. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  1151. vmcoreinfo_size += r;
  1152. }
  1153. /*
  1154. * provide an empty default implementation here -- architecture
  1155. * code may override this
  1156. */
  1157. void __weak arch_crash_save_vmcoreinfo(void)
  1158. {}
  1159. unsigned long __weak paddr_vmcoreinfo_note(void)
  1160. {
  1161. return __pa((unsigned long)(char *)&vmcoreinfo_note);
  1162. }
  1163. static int __init crash_save_vmcoreinfo_init(void)
  1164. {
  1165. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  1166. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  1167. VMCOREINFO_SYMBOL(init_uts_ns);
  1168. VMCOREINFO_SYMBOL(node_online_map);
  1169. #ifdef CONFIG_MMU
  1170. VMCOREINFO_SYMBOL(swapper_pg_dir);
  1171. #endif
  1172. VMCOREINFO_SYMBOL(_stext);
  1173. VMCOREINFO_SYMBOL(vmap_area_list);
  1174. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1175. VMCOREINFO_SYMBOL(mem_map);
  1176. VMCOREINFO_SYMBOL(contig_page_data);
  1177. #endif
  1178. #ifdef CONFIG_SPARSEMEM
  1179. VMCOREINFO_SYMBOL(mem_section);
  1180. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  1181. VMCOREINFO_STRUCT_SIZE(mem_section);
  1182. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  1183. #endif
  1184. VMCOREINFO_STRUCT_SIZE(page);
  1185. VMCOREINFO_STRUCT_SIZE(pglist_data);
  1186. VMCOREINFO_STRUCT_SIZE(zone);
  1187. VMCOREINFO_STRUCT_SIZE(free_area);
  1188. VMCOREINFO_STRUCT_SIZE(list_head);
  1189. VMCOREINFO_SIZE(nodemask_t);
  1190. VMCOREINFO_OFFSET(page, flags);
  1191. VMCOREINFO_OFFSET(page, _count);
  1192. VMCOREINFO_OFFSET(page, mapping);
  1193. VMCOREINFO_OFFSET(page, lru);
  1194. VMCOREINFO_OFFSET(page, _mapcount);
  1195. VMCOREINFO_OFFSET(page, private);
  1196. VMCOREINFO_OFFSET(pglist_data, node_zones);
  1197. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  1198. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1199. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  1200. #endif
  1201. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  1202. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  1203. VMCOREINFO_OFFSET(pglist_data, node_id);
  1204. VMCOREINFO_OFFSET(zone, free_area);
  1205. VMCOREINFO_OFFSET(zone, vm_stat);
  1206. VMCOREINFO_OFFSET(zone, spanned_pages);
  1207. VMCOREINFO_OFFSET(free_area, free_list);
  1208. VMCOREINFO_OFFSET(list_head, next);
  1209. VMCOREINFO_OFFSET(list_head, prev);
  1210. VMCOREINFO_OFFSET(vmap_area, va_start);
  1211. VMCOREINFO_OFFSET(vmap_area, list);
  1212. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  1213. log_buf_kexec_setup();
  1214. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  1215. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  1216. VMCOREINFO_NUMBER(PG_lru);
  1217. VMCOREINFO_NUMBER(PG_private);
  1218. VMCOREINFO_NUMBER(PG_swapcache);
  1219. VMCOREINFO_NUMBER(PG_slab);
  1220. #ifdef CONFIG_MEMORY_FAILURE
  1221. VMCOREINFO_NUMBER(PG_hwpoison);
  1222. #endif
  1223. VMCOREINFO_NUMBER(PG_head_mask);
  1224. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  1225. #ifdef CONFIG_X86
  1226. VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
  1227. #endif
  1228. #ifdef CONFIG_HUGETLBFS
  1229. VMCOREINFO_SYMBOL(free_huge_page);
  1230. #endif
  1231. arch_crash_save_vmcoreinfo();
  1232. update_vmcoreinfo_note();
  1233. return 0;
  1234. }
  1235. subsys_initcall(crash_save_vmcoreinfo_init);
  1236. /*
  1237. * Move into place and start executing a preloaded standalone
  1238. * executable. If nothing was preloaded return an error.
  1239. */
  1240. int kernel_kexec(void)
  1241. {
  1242. int error = 0;
  1243. if (!mutex_trylock(&kexec_mutex))
  1244. return -EBUSY;
  1245. if (!kexec_image) {
  1246. error = -EINVAL;
  1247. goto Unlock;
  1248. }
  1249. #ifdef CONFIG_KEXEC_JUMP
  1250. if (kexec_image->preserve_context) {
  1251. lock_system_sleep();
  1252. pm_prepare_console();
  1253. error = freeze_processes();
  1254. if (error) {
  1255. error = -EBUSY;
  1256. goto Restore_console;
  1257. }
  1258. suspend_console();
  1259. error = dpm_suspend_start(PMSG_FREEZE);
  1260. if (error)
  1261. goto Resume_console;
  1262. /* At this point, dpm_suspend_start() has been called,
  1263. * but *not* dpm_suspend_end(). We *must* call
  1264. * dpm_suspend_end() now. Otherwise, drivers for
  1265. * some devices (e.g. interrupt controllers) become
  1266. * desynchronized with the actual state of the
  1267. * hardware at resume time, and evil weirdness ensues.
  1268. */
  1269. error = dpm_suspend_end(PMSG_FREEZE);
  1270. if (error)
  1271. goto Resume_devices;
  1272. error = disable_nonboot_cpus();
  1273. if (error)
  1274. goto Enable_cpus;
  1275. local_irq_disable();
  1276. error = syscore_suspend();
  1277. if (error)
  1278. goto Enable_irqs;
  1279. } else
  1280. #endif
  1281. {
  1282. kexec_in_progress = true;
  1283. kernel_restart_prepare(NULL);
  1284. migrate_to_reboot_cpu();
  1285. /*
  1286. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  1287. * no further code needs to use CPU hotplug (which is true in
  1288. * the reboot case). However, the kexec path depends on using
  1289. * CPU hotplug again; so re-enable it here.
  1290. */
  1291. cpu_hotplug_enable();
  1292. pr_emerg("Starting new kernel\n");
  1293. machine_shutdown();
  1294. }
  1295. machine_kexec(kexec_image);
  1296. #ifdef CONFIG_KEXEC_JUMP
  1297. if (kexec_image->preserve_context) {
  1298. syscore_resume();
  1299. Enable_irqs:
  1300. local_irq_enable();
  1301. Enable_cpus:
  1302. enable_nonboot_cpus();
  1303. dpm_resume_start(PMSG_RESTORE);
  1304. Resume_devices:
  1305. dpm_resume_end(PMSG_RESTORE);
  1306. Resume_console:
  1307. resume_console();
  1308. thaw_processes();
  1309. Restore_console:
  1310. pm_restore_console();
  1311. unlock_system_sleep();
  1312. }
  1313. #endif
  1314. Unlock:
  1315. mutex_unlock(&kexec_mutex);
  1316. return error;
  1317. }
  1318. /*
  1319. * Add and remove page tables for crashkernel memory
  1320. *
  1321. * Provide an empty default implementation here -- architecture
  1322. * code may override this
  1323. */
  1324. void __weak crash_map_reserved_pages(void)
  1325. {}
  1326. void __weak crash_unmap_reserved_pages(void)
  1327. {}