hibernate.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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. pm_pr_dbg("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. pm_pr_dbg("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. pr_info("hibernation entry\n");
  590. pm_prepare_console();
  591. error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
  592. if (error) {
  593. nr_calls--;
  594. goto Exit;
  595. }
  596. pr_info("Syncing filesystems ... \n");
  597. sys_sync();
  598. pr_info("done.\n");
  599. error = freeze_processes();
  600. if (error)
  601. goto Exit;
  602. lock_device_hotplug();
  603. /* Allocate memory management structures */
  604. error = create_basic_memory_bitmaps();
  605. if (error)
  606. goto Thaw;
  607. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  608. if (error || freezer_test_done)
  609. goto Free_bitmaps;
  610. if (in_suspend) {
  611. unsigned int flags = 0;
  612. if (hibernation_mode == HIBERNATION_PLATFORM)
  613. flags |= SF_PLATFORM_MODE;
  614. if (nocompress)
  615. flags |= SF_NOCOMPRESS_MODE;
  616. else
  617. flags |= SF_CRC32_MODE;
  618. pm_pr_dbg("Writing image.\n");
  619. error = swsusp_write(flags);
  620. swsusp_free();
  621. if (!error) {
  622. if (hibernation_mode == HIBERNATION_TEST_RESUME)
  623. snapshot_test = true;
  624. else
  625. power_down();
  626. }
  627. in_suspend = 0;
  628. pm_restore_gfp_mask();
  629. } else {
  630. pm_pr_dbg("Image restored successfully.\n");
  631. }
  632. Free_bitmaps:
  633. free_basic_memory_bitmaps();
  634. Thaw:
  635. unlock_device_hotplug();
  636. if (snapshot_test) {
  637. pm_pr_dbg("Checking hibernation image\n");
  638. error = swsusp_check();
  639. if (!error)
  640. error = load_image_and_restore();
  641. }
  642. thaw_processes();
  643. /* Don't bother checking whether freezer_test_done is true */
  644. freezer_test_done = false;
  645. Exit:
  646. __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
  647. pm_restore_console();
  648. atomic_inc(&snapshot_device_available);
  649. Unlock:
  650. unlock_system_sleep();
  651. pr_info("hibernation exit\n");
  652. return error;
  653. }
  654. /**
  655. * software_resume - Resume from a saved hibernation image.
  656. *
  657. * This routine is called as a late initcall, when all devices have been
  658. * discovered and initialized already.
  659. *
  660. * The image reading code is called to see if there is a hibernation image
  661. * available for reading. If that is the case, devices are quiesced and the
  662. * contents of memory is restored from the saved image.
  663. *
  664. * If this is successful, control reappears in the restored target kernel in
  665. * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
  666. * attempts to recover gracefully and make the kernel return to the normal mode
  667. * of operation.
  668. */
  669. static int software_resume(void)
  670. {
  671. int error, nr_calls = 0;
  672. /*
  673. * If the user said "noresume".. bail out early.
  674. */
  675. if (noresume || !hibernation_available())
  676. return 0;
  677. /*
  678. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  679. * is configured into the kernel. Since the regular hibernate
  680. * trigger path is via sysfs which takes a buffer mutex before
  681. * calling hibernate functions (which take pm_mutex) this can
  682. * cause lockdep to complain about a possible ABBA deadlock
  683. * which cannot happen since we're in the boot code here and
  684. * sysfs can't be invoked yet. Therefore, we use a subclass
  685. * here to avoid lockdep complaining.
  686. */
  687. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  688. if (swsusp_resume_device)
  689. goto Check_image;
  690. if (!strlen(resume_file)) {
  691. error = -ENOENT;
  692. goto Unlock;
  693. }
  694. pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
  695. if (resume_delay) {
  696. pr_info("Waiting %dsec before reading resume device ...\n",
  697. resume_delay);
  698. ssleep(resume_delay);
  699. }
  700. /* Check if the device is there */
  701. swsusp_resume_device = name_to_dev_t(resume_file);
  702. /*
  703. * name_to_dev_t is ineffective to verify parition if resume_file is in
  704. * integer format. (e.g. major:minor)
  705. */
  706. if (isdigit(resume_file[0]) && resume_wait) {
  707. int partno;
  708. while (!get_gendisk(swsusp_resume_device, &partno))
  709. msleep(10);
  710. }
  711. if (!swsusp_resume_device) {
  712. /*
  713. * Some device discovery might still be in progress; we need
  714. * to wait for this to finish.
  715. */
  716. wait_for_device_probe();
  717. if (resume_wait) {
  718. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  719. msleep(10);
  720. async_synchronize_full();
  721. }
  722. swsusp_resume_device = name_to_dev_t(resume_file);
  723. if (!swsusp_resume_device) {
  724. error = -ENODEV;
  725. goto Unlock;
  726. }
  727. }
  728. Check_image:
  729. pm_pr_dbg("Hibernation image partition %d:%d present\n",
  730. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  731. pm_pr_dbg("Looking for hibernation image.\n");
  732. error = swsusp_check();
  733. if (error)
  734. goto Unlock;
  735. /* The snapshot device should not be opened while we're running */
  736. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  737. error = -EBUSY;
  738. swsusp_close(FMODE_READ);
  739. goto Unlock;
  740. }
  741. pr_info("resume from hibernation\n");
  742. pm_prepare_console();
  743. error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
  744. if (error) {
  745. nr_calls--;
  746. goto Close_Finish;
  747. }
  748. pm_pr_dbg("Preparing processes for restore.\n");
  749. error = freeze_processes();
  750. if (error)
  751. goto Close_Finish;
  752. error = load_image_and_restore();
  753. thaw_processes();
  754. Finish:
  755. __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
  756. pm_restore_console();
  757. pr_info("resume from hibernation failed (%d)\n", error);
  758. atomic_inc(&snapshot_device_available);
  759. /* For success case, the suspend path will release the lock */
  760. Unlock:
  761. mutex_unlock(&pm_mutex);
  762. pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
  763. return error;
  764. Close_Finish:
  765. swsusp_close(FMODE_READ);
  766. goto Finish;
  767. }
  768. late_initcall_sync(software_resume);
  769. static const char * const hibernation_modes[] = {
  770. [HIBERNATION_PLATFORM] = "platform",
  771. [HIBERNATION_SHUTDOWN] = "shutdown",
  772. [HIBERNATION_REBOOT] = "reboot",
  773. #ifdef CONFIG_SUSPEND
  774. [HIBERNATION_SUSPEND] = "suspend",
  775. #endif
  776. [HIBERNATION_TEST_RESUME] = "test_resume",
  777. };
  778. /*
  779. * /sys/power/disk - Control hibernation mode.
  780. *
  781. * Hibernation can be handled in several ways. There are a few different ways
  782. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  783. * or other hibernation_ops), powering it off or rebooting it (for testing
  784. * mostly).
  785. *
  786. * The sysfs file /sys/power/disk provides an interface for selecting the
  787. * hibernation mode to use. Reading from this file causes the available modes
  788. * to be printed. There are 3 modes that can be supported:
  789. *
  790. * 'platform'
  791. * 'shutdown'
  792. * 'reboot'
  793. *
  794. * If a platform hibernation driver is in use, 'platform' will be supported
  795. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  796. * The selected option (i.e. the one corresponding to the current value of
  797. * hibernation_mode) is enclosed by a square bracket.
  798. *
  799. * To select a given hibernation mode it is necessary to write the mode's
  800. * string representation (as returned by reading from /sys/power/disk) back
  801. * into /sys/power/disk.
  802. */
  803. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  804. char *buf)
  805. {
  806. int i;
  807. char *start = buf;
  808. if (!hibernation_available())
  809. return sprintf(buf, "[disabled]\n");
  810. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  811. if (!hibernation_modes[i])
  812. continue;
  813. switch (i) {
  814. case HIBERNATION_SHUTDOWN:
  815. case HIBERNATION_REBOOT:
  816. #ifdef CONFIG_SUSPEND
  817. case HIBERNATION_SUSPEND:
  818. #endif
  819. case HIBERNATION_TEST_RESUME:
  820. break;
  821. case HIBERNATION_PLATFORM:
  822. if (hibernation_ops)
  823. break;
  824. /* not a valid mode, continue with loop */
  825. continue;
  826. }
  827. if (i == hibernation_mode)
  828. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  829. else
  830. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  831. }
  832. buf += sprintf(buf, "\n");
  833. return buf-start;
  834. }
  835. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  836. const char *buf, size_t n)
  837. {
  838. int error = 0;
  839. int i;
  840. int len;
  841. char *p;
  842. int mode = HIBERNATION_INVALID;
  843. if (!hibernation_available())
  844. return -EPERM;
  845. p = memchr(buf, '\n', n);
  846. len = p ? p - buf : n;
  847. lock_system_sleep();
  848. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  849. if (len == strlen(hibernation_modes[i])
  850. && !strncmp(buf, hibernation_modes[i], len)) {
  851. mode = i;
  852. break;
  853. }
  854. }
  855. if (mode != HIBERNATION_INVALID) {
  856. switch (mode) {
  857. case HIBERNATION_SHUTDOWN:
  858. case HIBERNATION_REBOOT:
  859. #ifdef CONFIG_SUSPEND
  860. case HIBERNATION_SUSPEND:
  861. #endif
  862. case HIBERNATION_TEST_RESUME:
  863. hibernation_mode = mode;
  864. break;
  865. case HIBERNATION_PLATFORM:
  866. if (hibernation_ops)
  867. hibernation_mode = mode;
  868. else
  869. error = -EINVAL;
  870. }
  871. } else
  872. error = -EINVAL;
  873. if (!error)
  874. pm_pr_dbg("Hibernation mode set to '%s'\n",
  875. hibernation_modes[mode]);
  876. unlock_system_sleep();
  877. return error ? error : n;
  878. }
  879. power_attr(disk);
  880. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  881. char *buf)
  882. {
  883. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  884. MINOR(swsusp_resume_device));
  885. }
  886. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  887. const char *buf, size_t n)
  888. {
  889. dev_t res;
  890. int len = n;
  891. char *name;
  892. if (len && buf[len-1] == '\n')
  893. len--;
  894. name = kstrndup(buf, len, GFP_KERNEL);
  895. if (!name)
  896. return -ENOMEM;
  897. res = name_to_dev_t(name);
  898. kfree(name);
  899. if (!res)
  900. return -EINVAL;
  901. lock_system_sleep();
  902. swsusp_resume_device = res;
  903. unlock_system_sleep();
  904. pr_info("Starting manual resume from disk\n");
  905. noresume = 0;
  906. software_resume();
  907. return n;
  908. }
  909. power_attr(resume);
  910. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  911. char *buf)
  912. {
  913. return sprintf(buf, "%lu\n", image_size);
  914. }
  915. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  916. const char *buf, size_t n)
  917. {
  918. unsigned long size;
  919. if (sscanf(buf, "%lu", &size) == 1) {
  920. image_size = size;
  921. return n;
  922. }
  923. return -EINVAL;
  924. }
  925. power_attr(image_size);
  926. static ssize_t reserved_size_show(struct kobject *kobj,
  927. struct kobj_attribute *attr, char *buf)
  928. {
  929. return sprintf(buf, "%lu\n", reserved_size);
  930. }
  931. static ssize_t reserved_size_store(struct kobject *kobj,
  932. struct kobj_attribute *attr,
  933. const char *buf, size_t n)
  934. {
  935. unsigned long size;
  936. if (sscanf(buf, "%lu", &size) == 1) {
  937. reserved_size = size;
  938. return n;
  939. }
  940. return -EINVAL;
  941. }
  942. power_attr(reserved_size);
  943. static struct attribute * g[] = {
  944. &disk_attr.attr,
  945. &resume_attr.attr,
  946. &image_size_attr.attr,
  947. &reserved_size_attr.attr,
  948. NULL,
  949. };
  950. static const struct attribute_group attr_group = {
  951. .attrs = g,
  952. };
  953. static int __init pm_disk_init(void)
  954. {
  955. return sysfs_create_group(power_kobj, &attr_group);
  956. }
  957. core_initcall(pm_disk_init);
  958. static int __init resume_setup(char *str)
  959. {
  960. if (noresume)
  961. return 1;
  962. strncpy( resume_file, str, 255 );
  963. return 1;
  964. }
  965. static int __init resume_offset_setup(char *str)
  966. {
  967. unsigned long long offset;
  968. if (noresume)
  969. return 1;
  970. if (sscanf(str, "%llu", &offset) == 1)
  971. swsusp_resume_block = offset;
  972. return 1;
  973. }
  974. static int __init hibernate_setup(char *str)
  975. {
  976. if (!strncmp(str, "noresume", 8)) {
  977. noresume = 1;
  978. } else if (!strncmp(str, "nocompress", 10)) {
  979. nocompress = 1;
  980. } else if (!strncmp(str, "no", 2)) {
  981. noresume = 1;
  982. nohibernate = 1;
  983. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
  984. && !strncmp(str, "protect_image", 13)) {
  985. enable_restore_image_protection();
  986. }
  987. return 1;
  988. }
  989. static int __init noresume_setup(char *str)
  990. {
  991. noresume = 1;
  992. return 1;
  993. }
  994. static int __init resumewait_setup(char *str)
  995. {
  996. resume_wait = 1;
  997. return 1;
  998. }
  999. static int __init resumedelay_setup(char *str)
  1000. {
  1001. int rc = kstrtouint(str, 0, &resume_delay);
  1002. if (rc)
  1003. return rc;
  1004. return 1;
  1005. }
  1006. static int __init nohibernate_setup(char *str)
  1007. {
  1008. noresume = 1;
  1009. nohibernate = 1;
  1010. return 1;
  1011. }
  1012. __setup("noresume", noresume_setup);
  1013. __setup("resume_offset=", resume_offset_setup);
  1014. __setup("resume=", resume_setup);
  1015. __setup("hibernate=", hibernate_setup);
  1016. __setup("resumewait", resumewait_setup);
  1017. __setup("resumedelay=", resumedelay_setup);
  1018. __setup("nohibernate", nohibernate_setup);