hibernate.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
  8. * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
  9. *
  10. * This file is released under the GPLv2.
  11. */
  12. #define pr_fmt(fmt) "PM: " fmt
  13. #include <linux/export.h>
  14. #include <linux/suspend.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/reboot.h>
  17. #include <linux/string.h>
  18. #include <linux/device.h>
  19. #include <linux/async.h>
  20. #include <linux/delay.h>
  21. #include <linux/fs.h>
  22. #include <linux/mount.h>
  23. #include <linux/pm.h>
  24. #include <linux/nmi.h>
  25. #include <linux/console.h>
  26. #include <linux/cpu.h>
  27. #include <linux/freezer.h>
  28. #include <linux/gfp.h>
  29. #include <linux/syscore_ops.h>
  30. #include <linux/ctype.h>
  31. #include <linux/genhd.h>
  32. #include <linux/ktime.h>
  33. #include <trace/events/power.h>
  34. #include "power.h"
  35. static int nocompress;
  36. static int noresume;
  37. static int nohibernate;
  38. static int resume_wait;
  39. static unsigned int resume_delay;
  40. static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  41. dev_t swsusp_resume_device;
  42. sector_t swsusp_resume_block;
  43. __visible int in_suspend __nosavedata;
  44. enum {
  45. HIBERNATION_INVALID,
  46. HIBERNATION_PLATFORM,
  47. HIBERNATION_SHUTDOWN,
  48. HIBERNATION_REBOOT,
  49. #ifdef CONFIG_SUSPEND
  50. HIBERNATION_SUSPEND,
  51. #endif
  52. HIBERNATION_TEST_RESUME,
  53. /* keep last */
  54. __HIBERNATION_AFTER_LAST
  55. };
  56. #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  57. #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  58. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  59. bool freezer_test_done;
  60. static const struct platform_hibernation_ops *hibernation_ops;
  61. bool hibernation_available(void)
  62. {
  63. return (nohibernate == 0);
  64. }
  65. /**
  66. * hibernation_set_ops - Set the global hibernate operations.
  67. * @ops: Hibernation operations to use in subsequent hibernation transitions.
  68. */
  69. void hibernation_set_ops(const struct platform_hibernation_ops *ops)
  70. {
  71. if (ops && !(ops->begin && ops->end && ops->pre_snapshot
  72. && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  73. && ops->restore_cleanup && ops->leave)) {
  74. WARN_ON(1);
  75. return;
  76. }
  77. lock_system_sleep();
  78. hibernation_ops = ops;
  79. if (ops)
  80. hibernation_mode = HIBERNATION_PLATFORM;
  81. else if (hibernation_mode == HIBERNATION_PLATFORM)
  82. hibernation_mode = HIBERNATION_SHUTDOWN;
  83. unlock_system_sleep();
  84. }
  85. EXPORT_SYMBOL_GPL(hibernation_set_ops);
  86. static bool entering_platform_hibernation;
  87. bool system_entering_hibernation(void)
  88. {
  89. return entering_platform_hibernation;
  90. }
  91. EXPORT_SYMBOL(system_entering_hibernation);
  92. #ifdef CONFIG_PM_DEBUG
  93. static void hibernation_debug_sleep(void)
  94. {
  95. pr_info("hibernation debug: Waiting for 5 seconds.\n");
  96. mdelay(5000);
  97. }
  98. static int hibernation_test(int level)
  99. {
  100. if (pm_test_level == level) {
  101. hibernation_debug_sleep();
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. #else /* !CONFIG_PM_DEBUG */
  107. static int hibernation_test(int level) { return 0; }
  108. #endif /* !CONFIG_PM_DEBUG */
  109. /**
  110. * platform_begin - Call platform to start hibernation.
  111. * @platform_mode: Whether or not to use the platform driver.
  112. */
  113. static int platform_begin(int platform_mode)
  114. {
  115. return (platform_mode && hibernation_ops) ?
  116. hibernation_ops->begin() : 0;
  117. }
  118. /**
  119. * platform_end - Call platform to finish transition to the working state.
  120. * @platform_mode: Whether or not to use the platform driver.
  121. */
  122. static void platform_end(int platform_mode)
  123. {
  124. if (platform_mode && hibernation_ops)
  125. hibernation_ops->end();
  126. }
  127. /**
  128. * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
  129. * @platform_mode: Whether or not to use the platform driver.
  130. *
  131. * Use the platform driver to prepare the system for creating a hibernate image,
  132. * if so configured, and return an error code if that fails.
  133. */
  134. static int platform_pre_snapshot(int platform_mode)
  135. {
  136. return (platform_mode && hibernation_ops) ?
  137. hibernation_ops->pre_snapshot() : 0;
  138. }
  139. /**
  140. * platform_leave - Call platform to prepare a transition to the working state.
  141. * @platform_mode: Whether or not to use the platform driver.
  142. *
  143. * Use the platform driver prepare to prepare the machine for switching to the
  144. * normal mode of operation.
  145. *
  146. * This routine is called on one CPU with interrupts disabled.
  147. */
  148. static void platform_leave(int platform_mode)
  149. {
  150. if (platform_mode && hibernation_ops)
  151. hibernation_ops->leave();
  152. }
  153. /**
  154. * platform_finish - Call platform to switch the system to the working state.
  155. * @platform_mode: Whether or not to use the platform driver.
  156. *
  157. * Use the platform driver to switch the machine to the normal mode of
  158. * operation.
  159. *
  160. * This routine must be called after platform_prepare().
  161. */
  162. static void platform_finish(int platform_mode)
  163. {
  164. if (platform_mode && hibernation_ops)
  165. hibernation_ops->finish();
  166. }
  167. /**
  168. * platform_pre_restore - Prepare for hibernate image restoration.
  169. * @platform_mode: Whether or not to use the platform driver.
  170. *
  171. * Use the platform driver to prepare the system for resume from a hibernation
  172. * image.
  173. *
  174. * If the restore fails after this function has been called,
  175. * platform_restore_cleanup() must be called.
  176. */
  177. static int platform_pre_restore(int platform_mode)
  178. {
  179. return (platform_mode && hibernation_ops) ?
  180. hibernation_ops->pre_restore() : 0;
  181. }
  182. /**
  183. * platform_restore_cleanup - Switch to the working state after failing restore.
  184. * @platform_mode: Whether or not to use the platform driver.
  185. *
  186. * Use the platform driver to switch the system to the normal mode of operation
  187. * after a failing restore.
  188. *
  189. * If platform_pre_restore() has been called before the failing restore, this
  190. * function must be called too, regardless of the result of
  191. * platform_pre_restore().
  192. */
  193. static void platform_restore_cleanup(int platform_mode)
  194. {
  195. if (platform_mode && hibernation_ops)
  196. hibernation_ops->restore_cleanup();
  197. }
  198. /**
  199. * platform_recover - Recover from a failure to suspend devices.
  200. * @platform_mode: Whether or not to use the platform driver.
  201. */
  202. static void platform_recover(int platform_mode)
  203. {
  204. if (platform_mode && hibernation_ops && hibernation_ops->recover)
  205. hibernation_ops->recover();
  206. }
  207. /**
  208. * swsusp_show_speed - Print time elapsed between two events during hibernation.
  209. * @start: Starting event.
  210. * @stop: Final event.
  211. * @nr_pages: Number of memory pages processed between @start and @stop.
  212. * @msg: Additional diagnostic message to print.
  213. */
  214. void swsusp_show_speed(ktime_t start, ktime_t stop,
  215. unsigned nr_pages, char *msg)
  216. {
  217. ktime_t diff;
  218. u64 elapsed_centisecs64;
  219. unsigned int centisecs;
  220. unsigned int k;
  221. unsigned int kps;
  222. diff = ktime_sub(stop, start);
  223. elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
  224. centisecs = elapsed_centisecs64;
  225. if (centisecs == 0)
  226. centisecs = 1; /* avoid div-by-zero */
  227. k = nr_pages * (PAGE_SIZE / 1024);
  228. kps = (k * 100) / centisecs;
  229. pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
  230. msg, k, centisecs / 100, centisecs % 100, kps / 1000,
  231. (kps % 1000) / 10);
  232. }
  233. /**
  234. * create_image - Create a hibernation image.
  235. * @platform_mode: Whether or not to use the platform driver.
  236. *
  237. * Execute device drivers' "late" and "noirq" freeze callbacks, create a
  238. * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
  239. *
  240. * Control reappears in this routine after the subsequent restore.
  241. */
  242. static int create_image(int platform_mode)
  243. {
  244. int error;
  245. error = dpm_suspend_end(PMSG_FREEZE);
  246. if (error) {
  247. pr_err("Some devices failed to power down, aborting hibernation\n");
  248. return error;
  249. }
  250. error = platform_pre_snapshot(platform_mode);
  251. if (error || hibernation_test(TEST_PLATFORM))
  252. goto Platform_finish;
  253. error = disable_nonboot_cpus();
  254. if (error || hibernation_test(TEST_CPUS))
  255. goto Enable_cpus;
  256. local_irq_disable();
  257. error = syscore_suspend();
  258. if (error) {
  259. pr_err("Some system devices failed to power down, aborting hibernation\n");
  260. goto Enable_irqs;
  261. }
  262. if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
  263. goto Power_up;
  264. in_suspend = 1;
  265. save_processor_state();
  266. trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
  267. error = swsusp_arch_suspend();
  268. /* Restore control flow magically appears here */
  269. restore_processor_state();
  270. trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
  271. if (error)
  272. pr_err("Error %d creating hibernation image\n", error);
  273. if (!in_suspend) {
  274. events_check_enabled = false;
  275. clear_free_pages();
  276. }
  277. platform_leave(platform_mode);
  278. Power_up:
  279. syscore_resume();
  280. Enable_irqs:
  281. local_irq_enable();
  282. Enable_cpus:
  283. enable_nonboot_cpus();
  284. Platform_finish:
  285. platform_finish(platform_mode);
  286. dpm_resume_start(in_suspend ?
  287. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  288. return error;
  289. }
  290. /**
  291. * hibernation_snapshot - Quiesce devices and create a hibernation image.
  292. * @platform_mode: If set, use platform driver to prepare for the transition.
  293. *
  294. * This routine must be called with pm_mutex held.
  295. */
  296. int hibernation_snapshot(int platform_mode)
  297. {
  298. pm_message_t msg;
  299. int error;
  300. pm_suspend_clear_flags();
  301. error = platform_begin(platform_mode);
  302. if (error)
  303. goto Close;
  304. /* Preallocate image memory before shutting down devices. */
  305. error = hibernate_preallocate_memory();
  306. if (error)
  307. goto Close;
  308. error = freeze_kernel_threads();
  309. if (error)
  310. goto Cleanup;
  311. if (hibernation_test(TEST_FREEZER)) {
  312. /*
  313. * Indicate to the caller that we are returning due to a
  314. * successful freezer test.
  315. */
  316. freezer_test_done = true;
  317. goto Thaw;
  318. }
  319. error = dpm_prepare(PMSG_FREEZE);
  320. if (error) {
  321. dpm_complete(PMSG_RECOVER);
  322. goto Thaw;
  323. }
  324. suspend_console();
  325. pm_restrict_gfp_mask();
  326. error = dpm_suspend(PMSG_FREEZE);
  327. if (error || hibernation_test(TEST_DEVICES))
  328. platform_recover(platform_mode);
  329. else
  330. error = create_image(platform_mode);
  331. /*
  332. * In the case that we call create_image() above, the control
  333. * returns here (1) after the image has been created or the
  334. * image creation has failed and (2) after a successful restore.
  335. */
  336. /* We may need to release the preallocated image pages here. */
  337. if (error || !in_suspend)
  338. swsusp_free();
  339. msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
  340. dpm_resume(msg);
  341. if (error || !in_suspend)
  342. pm_restore_gfp_mask();
  343. resume_console();
  344. dpm_complete(msg);
  345. Close:
  346. platform_end(platform_mode);
  347. return error;
  348. Thaw:
  349. thaw_kernel_threads();
  350. Cleanup:
  351. swsusp_free();
  352. goto Close;
  353. }
  354. int __weak hibernate_resume_nonboot_cpu_disable(void)
  355. {
  356. return disable_nonboot_cpus();
  357. }
  358. /**
  359. * resume_target_kernel - Restore system state from a hibernation image.
  360. * @platform_mode: Whether or not to use the platform driver.
  361. *
  362. * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
  363. * contents of highmem that have not been restored yet from the image and run
  364. * the low-level code that will restore the remaining contents of memory and
  365. * switch to the just restored target kernel.
  366. */
  367. static int resume_target_kernel(bool platform_mode)
  368. {
  369. int error;
  370. error = dpm_suspend_end(PMSG_QUIESCE);
  371. if (error) {
  372. pr_err("Some devices failed to power down, aborting resume\n");
  373. return error;
  374. }
  375. error = platform_pre_restore(platform_mode);
  376. if (error)
  377. goto Cleanup;
  378. error = hibernate_resume_nonboot_cpu_disable();
  379. if (error)
  380. goto Enable_cpus;
  381. local_irq_disable();
  382. error = syscore_suspend();
  383. if (error)
  384. goto Enable_irqs;
  385. save_processor_state();
  386. error = restore_highmem();
  387. if (!error) {
  388. error = swsusp_arch_resume();
  389. /*
  390. * The code below is only ever reached in case of a failure.
  391. * Otherwise, execution continues at the place where
  392. * swsusp_arch_suspend() was called.
  393. */
  394. BUG_ON(!error);
  395. /*
  396. * This call to restore_highmem() reverts the changes made by
  397. * the previous one.
  398. */
  399. restore_highmem();
  400. }
  401. /*
  402. * The only reason why swsusp_arch_resume() can fail is memory being
  403. * very tight, so we have to free it as soon as we can to avoid
  404. * subsequent failures.
  405. */
  406. swsusp_free();
  407. restore_processor_state();
  408. touch_softlockup_watchdog();
  409. syscore_resume();
  410. Enable_irqs:
  411. local_irq_enable();
  412. Enable_cpus:
  413. enable_nonboot_cpus();
  414. Cleanup:
  415. platform_restore_cleanup(platform_mode);
  416. dpm_resume_start(PMSG_RECOVER);
  417. return error;
  418. }
  419. /**
  420. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  421. * @platform_mode: If set, use platform driver to prepare for the transition.
  422. *
  423. * This routine must be called with pm_mutex held. If it is successful, control
  424. * reappears in the restored target kernel in hibernation_snapshot().
  425. */
  426. int hibernation_restore(int platform_mode)
  427. {
  428. int error;
  429. pm_prepare_console();
  430. suspend_console();
  431. pm_restrict_gfp_mask();
  432. error = dpm_suspend_start(PMSG_QUIESCE);
  433. if (!error) {
  434. error = resume_target_kernel(platform_mode);
  435. /*
  436. * The above should either succeed and jump to the new kernel,
  437. * or return with an error. Otherwise things are just
  438. * undefined, so let's be paranoid.
  439. */
  440. BUG_ON(!error);
  441. }
  442. dpm_resume_end(PMSG_RECOVER);
  443. pm_restore_gfp_mask();
  444. resume_console();
  445. pm_restore_console();
  446. return error;
  447. }
  448. /**
  449. * hibernation_platform_enter - Power off the system using the platform driver.
  450. */
  451. int hibernation_platform_enter(void)
  452. {
  453. int error;
  454. if (!hibernation_ops)
  455. return -ENOSYS;
  456. /*
  457. * We have cancelled the power transition by running
  458. * hibernation_ops->finish() before saving the image, so we should let
  459. * the firmware know that we're going to enter the sleep state after all
  460. */
  461. error = hibernation_ops->begin();
  462. if (error)
  463. goto Close;
  464. entering_platform_hibernation = true;
  465. suspend_console();
  466. error = dpm_suspend_start(PMSG_HIBERNATE);
  467. if (error) {
  468. if (hibernation_ops->recover)
  469. hibernation_ops->recover();
  470. goto Resume_devices;
  471. }
  472. error = dpm_suspend_end(PMSG_HIBERNATE);
  473. if (error)
  474. goto Resume_devices;
  475. error = hibernation_ops->prepare();
  476. if (error)
  477. goto Platform_finish;
  478. error = disable_nonboot_cpus();
  479. if (error)
  480. goto Enable_cpus;
  481. local_irq_disable();
  482. syscore_suspend();
  483. if (pm_wakeup_pending()) {
  484. error = -EAGAIN;
  485. goto Power_up;
  486. }
  487. hibernation_ops->enter();
  488. /* We should never get here */
  489. while (1);
  490. Power_up:
  491. syscore_resume();
  492. local_irq_enable();
  493. Enable_cpus:
  494. enable_nonboot_cpus();
  495. Platform_finish:
  496. hibernation_ops->finish();
  497. dpm_resume_start(PMSG_RESTORE);
  498. Resume_devices:
  499. entering_platform_hibernation = false;
  500. dpm_resume_end(PMSG_RESTORE);
  501. resume_console();
  502. Close:
  503. hibernation_ops->end();
  504. return error;
  505. }
  506. /**
  507. * power_down - Shut the machine down for hibernation.
  508. *
  509. * Use the platform driver, if configured, to put the system into the sleep
  510. * state corresponding to hibernation, or try to power it off or reboot,
  511. * depending on the value of hibernation_mode.
  512. */
  513. static void power_down(void)
  514. {
  515. #ifdef CONFIG_SUSPEND
  516. int error;
  517. if (hibernation_mode == HIBERNATION_SUSPEND) {
  518. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  519. if (error) {
  520. hibernation_mode = hibernation_ops ?
  521. HIBERNATION_PLATFORM :
  522. HIBERNATION_SHUTDOWN;
  523. } else {
  524. /* Restore swap signature. */
  525. error = swsusp_unmark();
  526. if (error)
  527. pr_err("Swap will be unusable! Try swapon -a.\n");
  528. return;
  529. }
  530. }
  531. #endif
  532. switch (hibernation_mode) {
  533. case HIBERNATION_REBOOT:
  534. kernel_restart(NULL);
  535. break;
  536. case HIBERNATION_PLATFORM:
  537. hibernation_platform_enter();
  538. case HIBERNATION_SHUTDOWN:
  539. if (pm_power_off)
  540. kernel_power_off();
  541. break;
  542. }
  543. kernel_halt();
  544. /*
  545. * Valid image is on the disk, if we continue we risk serious data
  546. * corruption after resume.
  547. */
  548. pr_crit("Power down manually\n");
  549. while (1)
  550. cpu_relax();
  551. }
  552. static int load_image_and_restore(void)
  553. {
  554. int error;
  555. unsigned int flags;
  556. pr_debug("Loading hibernation image.\n");
  557. lock_device_hotplug();
  558. error = create_basic_memory_bitmaps();
  559. if (error)
  560. goto Unlock;
  561. error = swsusp_read(&flags);
  562. swsusp_close(FMODE_READ);
  563. if (!error)
  564. hibernation_restore(flags & SF_PLATFORM_MODE);
  565. pr_err("Failed to load hibernation image, recovering.\n");
  566. swsusp_free();
  567. free_basic_memory_bitmaps();
  568. Unlock:
  569. unlock_device_hotplug();
  570. return error;
  571. }
  572. /**
  573. * hibernate - Carry out system hibernation, including saving the image.
  574. */
  575. int hibernate(void)
  576. {
  577. int error, nr_calls = 0;
  578. bool snapshot_test = false;
  579. if (!hibernation_available()) {
  580. pr_debug("Hibernation not available.\n");
  581. return -EPERM;
  582. }
  583. lock_system_sleep();
  584. /* The snapshot device should not be opened while we're running */
  585. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  586. error = -EBUSY;
  587. goto Unlock;
  588. }
  589. pm_prepare_console();
  590. error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
  591. if (error) {
  592. nr_calls--;
  593. goto Exit;
  594. }
  595. pr_info("Syncing filesystems ... \n");
  596. sys_sync();
  597. pr_info("done.\n");
  598. error = freeze_processes();
  599. if (error)
  600. goto Exit;
  601. lock_device_hotplug();
  602. /* Allocate memory management structures */
  603. error = create_basic_memory_bitmaps();
  604. if (error)
  605. goto Thaw;
  606. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  607. if (error || freezer_test_done)
  608. goto Free_bitmaps;
  609. if (in_suspend) {
  610. unsigned int flags = 0;
  611. if (hibernation_mode == HIBERNATION_PLATFORM)
  612. flags |= SF_PLATFORM_MODE;
  613. if (nocompress)
  614. flags |= SF_NOCOMPRESS_MODE;
  615. else
  616. flags |= SF_CRC32_MODE;
  617. pr_debug("Writing image.\n");
  618. error = swsusp_write(flags);
  619. swsusp_free();
  620. if (!error) {
  621. if (hibernation_mode == HIBERNATION_TEST_RESUME)
  622. snapshot_test = true;
  623. else
  624. power_down();
  625. }
  626. in_suspend = 0;
  627. pm_restore_gfp_mask();
  628. } else {
  629. pr_debug("Image restored successfully.\n");
  630. }
  631. Free_bitmaps:
  632. free_basic_memory_bitmaps();
  633. Thaw:
  634. unlock_device_hotplug();
  635. if (snapshot_test) {
  636. pr_debug("Checking hibernation image\n");
  637. error = swsusp_check();
  638. if (!error)
  639. error = load_image_and_restore();
  640. }
  641. thaw_processes();
  642. /* Don't bother checking whether freezer_test_done is true */
  643. freezer_test_done = false;
  644. Exit:
  645. __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
  646. pm_restore_console();
  647. atomic_inc(&snapshot_device_available);
  648. Unlock:
  649. unlock_system_sleep();
  650. return error;
  651. }
  652. /**
  653. * software_resume - Resume from a saved hibernation image.
  654. *
  655. * This routine is called as a late initcall, when all devices have been
  656. * discovered and initialized already.
  657. *
  658. * The image reading code is called to see if there is a hibernation image
  659. * available for reading. If that is the case, devices are quiesced and the
  660. * contents of memory is restored from the saved image.
  661. *
  662. * If this is successful, control reappears in the restored target kernel in
  663. * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
  664. * attempts to recover gracefully and make the kernel return to the normal mode
  665. * of operation.
  666. */
  667. static int software_resume(void)
  668. {
  669. int error, nr_calls = 0;
  670. /*
  671. * If the user said "noresume".. bail out early.
  672. */
  673. if (noresume || !hibernation_available())
  674. return 0;
  675. /*
  676. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  677. * is configured into the kernel. Since the regular hibernate
  678. * trigger path is via sysfs which takes a buffer mutex before
  679. * calling hibernate functions (which take pm_mutex) this can
  680. * cause lockdep to complain about a possible ABBA deadlock
  681. * which cannot happen since we're in the boot code here and
  682. * sysfs can't be invoked yet. Therefore, we use a subclass
  683. * here to avoid lockdep complaining.
  684. */
  685. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  686. if (swsusp_resume_device)
  687. goto Check_image;
  688. if (!strlen(resume_file)) {
  689. error = -ENOENT;
  690. goto Unlock;
  691. }
  692. pr_debug("Checking hibernation image partition %s\n", resume_file);
  693. if (resume_delay) {
  694. pr_info("Waiting %dsec before reading resume device ...\n",
  695. resume_delay);
  696. ssleep(resume_delay);
  697. }
  698. /* Check if the device is there */
  699. swsusp_resume_device = name_to_dev_t(resume_file);
  700. /*
  701. * name_to_dev_t is ineffective to verify parition if resume_file is in
  702. * integer format. (e.g. major:minor)
  703. */
  704. if (isdigit(resume_file[0]) && resume_wait) {
  705. int partno;
  706. while (!get_gendisk(swsusp_resume_device, &partno))
  707. msleep(10);
  708. }
  709. if (!swsusp_resume_device) {
  710. /*
  711. * Some device discovery might still be in progress; we need
  712. * to wait for this to finish.
  713. */
  714. wait_for_device_probe();
  715. if (resume_wait) {
  716. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  717. msleep(10);
  718. async_synchronize_full();
  719. }
  720. swsusp_resume_device = name_to_dev_t(resume_file);
  721. if (!swsusp_resume_device) {
  722. error = -ENODEV;
  723. goto Unlock;
  724. }
  725. }
  726. Check_image:
  727. pr_debug("Hibernation image partition %d:%d present\n",
  728. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  729. pr_debug("Looking for hibernation image.\n");
  730. error = swsusp_check();
  731. if (error)
  732. goto Unlock;
  733. /* The snapshot device should not be opened while we're running */
  734. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  735. error = -EBUSY;
  736. swsusp_close(FMODE_READ);
  737. goto Unlock;
  738. }
  739. pm_prepare_console();
  740. error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
  741. if (error) {
  742. nr_calls--;
  743. goto Close_Finish;
  744. }
  745. pr_debug("Preparing processes for restore.\n");
  746. error = freeze_processes();
  747. if (error)
  748. goto Close_Finish;
  749. error = load_image_and_restore();
  750. thaw_processes();
  751. Finish:
  752. __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
  753. pm_restore_console();
  754. atomic_inc(&snapshot_device_available);
  755. /* For success case, the suspend path will release the lock */
  756. Unlock:
  757. mutex_unlock(&pm_mutex);
  758. pr_debug("Hibernation image not present or could not be loaded.\n");
  759. return error;
  760. Close_Finish:
  761. swsusp_close(FMODE_READ);
  762. goto Finish;
  763. }
  764. late_initcall_sync(software_resume);
  765. static const char * const hibernation_modes[] = {
  766. [HIBERNATION_PLATFORM] = "platform",
  767. [HIBERNATION_SHUTDOWN] = "shutdown",
  768. [HIBERNATION_REBOOT] = "reboot",
  769. #ifdef CONFIG_SUSPEND
  770. [HIBERNATION_SUSPEND] = "suspend",
  771. #endif
  772. [HIBERNATION_TEST_RESUME] = "test_resume",
  773. };
  774. /*
  775. * /sys/power/disk - Control hibernation mode.
  776. *
  777. * Hibernation can be handled in several ways. There are a few different ways
  778. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  779. * or other hibernation_ops), powering it off or rebooting it (for testing
  780. * mostly).
  781. *
  782. * The sysfs file /sys/power/disk provides an interface for selecting the
  783. * hibernation mode to use. Reading from this file causes the available modes
  784. * to be printed. There are 3 modes that can be supported:
  785. *
  786. * 'platform'
  787. * 'shutdown'
  788. * 'reboot'
  789. *
  790. * If a platform hibernation driver is in use, 'platform' will be supported
  791. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  792. * The selected option (i.e. the one corresponding to the current value of
  793. * hibernation_mode) is enclosed by a square bracket.
  794. *
  795. * To select a given hibernation mode it is necessary to write the mode's
  796. * string representation (as returned by reading from /sys/power/disk) back
  797. * into /sys/power/disk.
  798. */
  799. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  800. char *buf)
  801. {
  802. int i;
  803. char *start = buf;
  804. if (!hibernation_available())
  805. return sprintf(buf, "[disabled]\n");
  806. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  807. if (!hibernation_modes[i])
  808. continue;
  809. switch (i) {
  810. case HIBERNATION_SHUTDOWN:
  811. case HIBERNATION_REBOOT:
  812. #ifdef CONFIG_SUSPEND
  813. case HIBERNATION_SUSPEND:
  814. #endif
  815. case HIBERNATION_TEST_RESUME:
  816. break;
  817. case HIBERNATION_PLATFORM:
  818. if (hibernation_ops)
  819. break;
  820. /* not a valid mode, continue with loop */
  821. continue;
  822. }
  823. if (i == hibernation_mode)
  824. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  825. else
  826. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  827. }
  828. buf += sprintf(buf, "\n");
  829. return buf-start;
  830. }
  831. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  832. const char *buf, size_t n)
  833. {
  834. int error = 0;
  835. int i;
  836. int len;
  837. char *p;
  838. int mode = HIBERNATION_INVALID;
  839. if (!hibernation_available())
  840. return -EPERM;
  841. p = memchr(buf, '\n', n);
  842. len = p ? p - buf : n;
  843. lock_system_sleep();
  844. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  845. if (len == strlen(hibernation_modes[i])
  846. && !strncmp(buf, hibernation_modes[i], len)) {
  847. mode = i;
  848. break;
  849. }
  850. }
  851. if (mode != HIBERNATION_INVALID) {
  852. switch (mode) {
  853. case HIBERNATION_SHUTDOWN:
  854. case HIBERNATION_REBOOT:
  855. #ifdef CONFIG_SUSPEND
  856. case HIBERNATION_SUSPEND:
  857. #endif
  858. case HIBERNATION_TEST_RESUME:
  859. hibernation_mode = mode;
  860. break;
  861. case HIBERNATION_PLATFORM:
  862. if (hibernation_ops)
  863. hibernation_mode = mode;
  864. else
  865. error = -EINVAL;
  866. }
  867. } else
  868. error = -EINVAL;
  869. if (!error)
  870. pr_debug("Hibernation mode set to '%s'\n",
  871. hibernation_modes[mode]);
  872. unlock_system_sleep();
  873. return error ? error : n;
  874. }
  875. power_attr(disk);
  876. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  877. char *buf)
  878. {
  879. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  880. MINOR(swsusp_resume_device));
  881. }
  882. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  883. const char *buf, size_t n)
  884. {
  885. dev_t res;
  886. int len = n;
  887. char *name;
  888. if (len && buf[len-1] == '\n')
  889. len--;
  890. name = kstrndup(buf, len, GFP_KERNEL);
  891. if (!name)
  892. return -ENOMEM;
  893. res = name_to_dev_t(name);
  894. kfree(name);
  895. if (!res)
  896. return -EINVAL;
  897. lock_system_sleep();
  898. swsusp_resume_device = res;
  899. unlock_system_sleep();
  900. pr_info("Starting manual resume from disk\n");
  901. noresume = 0;
  902. software_resume();
  903. return n;
  904. }
  905. power_attr(resume);
  906. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  907. char *buf)
  908. {
  909. return sprintf(buf, "%lu\n", image_size);
  910. }
  911. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  912. const char *buf, size_t n)
  913. {
  914. unsigned long size;
  915. if (sscanf(buf, "%lu", &size) == 1) {
  916. image_size = size;
  917. return n;
  918. }
  919. return -EINVAL;
  920. }
  921. power_attr(image_size);
  922. static ssize_t reserved_size_show(struct kobject *kobj,
  923. struct kobj_attribute *attr, char *buf)
  924. {
  925. return sprintf(buf, "%lu\n", reserved_size);
  926. }
  927. static ssize_t reserved_size_store(struct kobject *kobj,
  928. struct kobj_attribute *attr,
  929. const char *buf, size_t n)
  930. {
  931. unsigned long size;
  932. if (sscanf(buf, "%lu", &size) == 1) {
  933. reserved_size = size;
  934. return n;
  935. }
  936. return -EINVAL;
  937. }
  938. power_attr(reserved_size);
  939. static struct attribute * g[] = {
  940. &disk_attr.attr,
  941. &resume_attr.attr,
  942. &image_size_attr.attr,
  943. &reserved_size_attr.attr,
  944. NULL,
  945. };
  946. static const struct attribute_group attr_group = {
  947. .attrs = g,
  948. };
  949. static int __init pm_disk_init(void)
  950. {
  951. return sysfs_create_group(power_kobj, &attr_group);
  952. }
  953. core_initcall(pm_disk_init);
  954. static int __init resume_setup(char *str)
  955. {
  956. if (noresume)
  957. return 1;
  958. strncpy( resume_file, str, 255 );
  959. return 1;
  960. }
  961. static int __init resume_offset_setup(char *str)
  962. {
  963. unsigned long long offset;
  964. if (noresume)
  965. return 1;
  966. if (sscanf(str, "%llu", &offset) == 1)
  967. swsusp_resume_block = offset;
  968. return 1;
  969. }
  970. static int __init hibernate_setup(char *str)
  971. {
  972. if (!strncmp(str, "noresume", 8)) {
  973. noresume = 1;
  974. } else if (!strncmp(str, "nocompress", 10)) {
  975. nocompress = 1;
  976. } else if (!strncmp(str, "no", 2)) {
  977. noresume = 1;
  978. nohibernate = 1;
  979. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
  980. && !strncmp(str, "protect_image", 13)) {
  981. enable_restore_image_protection();
  982. }
  983. return 1;
  984. }
  985. static int __init noresume_setup(char *str)
  986. {
  987. noresume = 1;
  988. return 1;
  989. }
  990. static int __init resumewait_setup(char *str)
  991. {
  992. resume_wait = 1;
  993. return 1;
  994. }
  995. static int __init resumedelay_setup(char *str)
  996. {
  997. int rc = kstrtouint(str, 0, &resume_delay);
  998. if (rc)
  999. return rc;
  1000. return 1;
  1001. }
  1002. static int __init nohibernate_setup(char *str)
  1003. {
  1004. noresume = 1;
  1005. nohibernate = 1;
  1006. return 1;
  1007. }
  1008. __setup("noresume", noresume_setup);
  1009. __setup("resume_offset=", resume_offset_setup);
  1010. __setup("resume=", resume_setup);
  1011. __setup("hibernate=", hibernate_setup);
  1012. __setup("resumewait", resumewait_setup);
  1013. __setup("resumedelay=", resumedelay_setup);
  1014. __setup("nohibernate", nohibernate_setup);