hibernate.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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. __weak int arch_resume_nosmt(void)
  234. {
  235. return 0;
  236. }
  237. /**
  238. * create_image - Create a hibernation image.
  239. * @platform_mode: Whether or not to use the platform driver.
  240. *
  241. * Execute device drivers' "late" and "noirq" freeze callbacks, create a
  242. * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
  243. *
  244. * Control reappears in this routine after the subsequent restore.
  245. */
  246. static int create_image(int platform_mode)
  247. {
  248. int error;
  249. error = dpm_suspend_end(PMSG_FREEZE);
  250. if (error) {
  251. pr_err("Some devices failed to power down, aborting hibernation\n");
  252. return error;
  253. }
  254. error = platform_pre_snapshot(platform_mode);
  255. if (error || hibernation_test(TEST_PLATFORM))
  256. goto Platform_finish;
  257. error = disable_nonboot_cpus();
  258. if (error || hibernation_test(TEST_CPUS))
  259. goto Enable_cpus;
  260. local_irq_disable();
  261. error = syscore_suspend();
  262. if (error) {
  263. pr_err("Some system devices failed to power down, aborting hibernation\n");
  264. goto Enable_irqs;
  265. }
  266. if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
  267. goto Power_up;
  268. in_suspend = 1;
  269. save_processor_state();
  270. trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
  271. error = swsusp_arch_suspend();
  272. /* Restore control flow magically appears here */
  273. restore_processor_state();
  274. trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
  275. if (error)
  276. pr_err("Error %d creating hibernation image\n", error);
  277. if (!in_suspend) {
  278. events_check_enabled = false;
  279. clear_free_pages();
  280. }
  281. platform_leave(platform_mode);
  282. Power_up:
  283. syscore_resume();
  284. Enable_irqs:
  285. local_irq_enable();
  286. Enable_cpus:
  287. enable_nonboot_cpus();
  288. /* Allow architectures to do nosmt-specific post-resume dances */
  289. if (!in_suspend)
  290. error = arch_resume_nosmt();
  291. Platform_finish:
  292. platform_finish(platform_mode);
  293. dpm_resume_start(in_suspend ?
  294. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  295. return error;
  296. }
  297. /**
  298. * hibernation_snapshot - Quiesce devices and create a hibernation image.
  299. * @platform_mode: If set, use platform driver to prepare for the transition.
  300. *
  301. * This routine must be called with pm_mutex held.
  302. */
  303. int hibernation_snapshot(int platform_mode)
  304. {
  305. pm_message_t msg;
  306. int error;
  307. pm_suspend_clear_flags();
  308. error = platform_begin(platform_mode);
  309. if (error)
  310. goto Close;
  311. /* Preallocate image memory before shutting down devices. */
  312. error = hibernate_preallocate_memory();
  313. if (error)
  314. goto Close;
  315. error = freeze_kernel_threads();
  316. if (error)
  317. goto Cleanup;
  318. if (hibernation_test(TEST_FREEZER)) {
  319. /*
  320. * Indicate to the caller that we are returning due to a
  321. * successful freezer test.
  322. */
  323. freezer_test_done = true;
  324. goto Thaw;
  325. }
  326. error = dpm_prepare(PMSG_FREEZE);
  327. if (error) {
  328. dpm_complete(PMSG_RECOVER);
  329. goto Thaw;
  330. }
  331. suspend_console();
  332. pm_restrict_gfp_mask();
  333. error = dpm_suspend(PMSG_FREEZE);
  334. if (error || hibernation_test(TEST_DEVICES))
  335. platform_recover(platform_mode);
  336. else
  337. error = create_image(platform_mode);
  338. /*
  339. * In the case that we call create_image() above, the control
  340. * returns here (1) after the image has been created or the
  341. * image creation has failed and (2) after a successful restore.
  342. */
  343. /* We may need to release the preallocated image pages here. */
  344. if (error || !in_suspend)
  345. swsusp_free();
  346. msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
  347. dpm_resume(msg);
  348. if (error || !in_suspend)
  349. pm_restore_gfp_mask();
  350. resume_console();
  351. dpm_complete(msg);
  352. Close:
  353. platform_end(platform_mode);
  354. return error;
  355. Thaw:
  356. thaw_kernel_threads();
  357. Cleanup:
  358. swsusp_free();
  359. goto Close;
  360. }
  361. int __weak hibernate_resume_nonboot_cpu_disable(void)
  362. {
  363. return disable_nonboot_cpus();
  364. }
  365. /**
  366. * resume_target_kernel - Restore system state from a hibernation image.
  367. * @platform_mode: Whether or not to use the platform driver.
  368. *
  369. * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
  370. * contents of highmem that have not been restored yet from the image and run
  371. * the low-level code that will restore the remaining contents of memory and
  372. * switch to the just restored target kernel.
  373. */
  374. static int resume_target_kernel(bool platform_mode)
  375. {
  376. int error;
  377. error = dpm_suspend_end(PMSG_QUIESCE);
  378. if (error) {
  379. pr_err("Some devices failed to power down, aborting resume\n");
  380. return error;
  381. }
  382. error = platform_pre_restore(platform_mode);
  383. if (error)
  384. goto Cleanup;
  385. error = hibernate_resume_nonboot_cpu_disable();
  386. if (error)
  387. goto Enable_cpus;
  388. local_irq_disable();
  389. error = syscore_suspend();
  390. if (error)
  391. goto Enable_irqs;
  392. save_processor_state();
  393. error = restore_highmem();
  394. if (!error) {
  395. error = swsusp_arch_resume();
  396. /*
  397. * The code below is only ever reached in case of a failure.
  398. * Otherwise, execution continues at the place where
  399. * swsusp_arch_suspend() was called.
  400. */
  401. BUG_ON(!error);
  402. /*
  403. * This call to restore_highmem() reverts the changes made by
  404. * the previous one.
  405. */
  406. restore_highmem();
  407. }
  408. /*
  409. * The only reason why swsusp_arch_resume() can fail is memory being
  410. * very tight, so we have to free it as soon as we can to avoid
  411. * subsequent failures.
  412. */
  413. swsusp_free();
  414. restore_processor_state();
  415. touch_softlockup_watchdog();
  416. syscore_resume();
  417. Enable_irqs:
  418. local_irq_enable();
  419. Enable_cpus:
  420. enable_nonboot_cpus();
  421. Cleanup:
  422. platform_restore_cleanup(platform_mode);
  423. dpm_resume_start(PMSG_RECOVER);
  424. return error;
  425. }
  426. /**
  427. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  428. * @platform_mode: If set, use platform driver to prepare for the transition.
  429. *
  430. * This routine must be called with pm_mutex held. If it is successful, control
  431. * reappears in the restored target kernel in hibernation_snapshot().
  432. */
  433. int hibernation_restore(int platform_mode)
  434. {
  435. int error;
  436. pm_prepare_console();
  437. suspend_console();
  438. pm_restrict_gfp_mask();
  439. error = dpm_suspend_start(PMSG_QUIESCE);
  440. if (!error) {
  441. error = resume_target_kernel(platform_mode);
  442. /*
  443. * The above should either succeed and jump to the new kernel,
  444. * or return with an error. Otherwise things are just
  445. * undefined, so let's be paranoid.
  446. */
  447. BUG_ON(!error);
  448. }
  449. dpm_resume_end(PMSG_RECOVER);
  450. pm_restore_gfp_mask();
  451. resume_console();
  452. pm_restore_console();
  453. return error;
  454. }
  455. /**
  456. * hibernation_platform_enter - Power off the system using the platform driver.
  457. */
  458. int hibernation_platform_enter(void)
  459. {
  460. int error;
  461. if (!hibernation_ops)
  462. return -ENOSYS;
  463. /*
  464. * We have cancelled the power transition by running
  465. * hibernation_ops->finish() before saving the image, so we should let
  466. * the firmware know that we're going to enter the sleep state after all
  467. */
  468. error = hibernation_ops->begin();
  469. if (error)
  470. goto Close;
  471. entering_platform_hibernation = true;
  472. suspend_console();
  473. error = dpm_suspend_start(PMSG_HIBERNATE);
  474. if (error) {
  475. if (hibernation_ops->recover)
  476. hibernation_ops->recover();
  477. goto Resume_devices;
  478. }
  479. error = dpm_suspend_end(PMSG_HIBERNATE);
  480. if (error)
  481. goto Resume_devices;
  482. error = hibernation_ops->prepare();
  483. if (error)
  484. goto Platform_finish;
  485. error = disable_nonboot_cpus();
  486. if (error)
  487. goto Enable_cpus;
  488. local_irq_disable();
  489. syscore_suspend();
  490. if (pm_wakeup_pending()) {
  491. error = -EAGAIN;
  492. goto Power_up;
  493. }
  494. hibernation_ops->enter();
  495. /* We should never get here */
  496. while (1);
  497. Power_up:
  498. syscore_resume();
  499. local_irq_enable();
  500. Enable_cpus:
  501. enable_nonboot_cpus();
  502. Platform_finish:
  503. hibernation_ops->finish();
  504. dpm_resume_start(PMSG_RESTORE);
  505. Resume_devices:
  506. entering_platform_hibernation = false;
  507. dpm_resume_end(PMSG_RESTORE);
  508. resume_console();
  509. Close:
  510. hibernation_ops->end();
  511. return error;
  512. }
  513. /**
  514. * power_down - Shut the machine down for hibernation.
  515. *
  516. * Use the platform driver, if configured, to put the system into the sleep
  517. * state corresponding to hibernation, or try to power it off or reboot,
  518. * depending on the value of hibernation_mode.
  519. */
  520. static void power_down(void)
  521. {
  522. #ifdef CONFIG_SUSPEND
  523. int error;
  524. if (hibernation_mode == HIBERNATION_SUSPEND) {
  525. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  526. if (error) {
  527. hibernation_mode = hibernation_ops ?
  528. HIBERNATION_PLATFORM :
  529. HIBERNATION_SHUTDOWN;
  530. } else {
  531. /* Restore swap signature. */
  532. error = swsusp_unmark();
  533. if (error)
  534. pr_err("Swap will be unusable! Try swapon -a.\n");
  535. return;
  536. }
  537. }
  538. #endif
  539. switch (hibernation_mode) {
  540. case HIBERNATION_REBOOT:
  541. kernel_restart(NULL);
  542. break;
  543. case HIBERNATION_PLATFORM:
  544. hibernation_platform_enter();
  545. case HIBERNATION_SHUTDOWN:
  546. if (pm_power_off)
  547. kernel_power_off();
  548. break;
  549. }
  550. kernel_halt();
  551. /*
  552. * Valid image is on the disk, if we continue we risk serious data
  553. * corruption after resume.
  554. */
  555. pr_crit("Power down manually\n");
  556. while (1)
  557. cpu_relax();
  558. }
  559. static int load_image_and_restore(void)
  560. {
  561. int error;
  562. unsigned int flags;
  563. pm_pr_dbg("Loading hibernation image.\n");
  564. lock_device_hotplug();
  565. error = create_basic_memory_bitmaps();
  566. if (error)
  567. goto Unlock;
  568. error = swsusp_read(&flags);
  569. swsusp_close(FMODE_READ);
  570. if (!error)
  571. hibernation_restore(flags & SF_PLATFORM_MODE);
  572. pr_err("Failed to load hibernation image, recovering.\n");
  573. swsusp_free();
  574. free_basic_memory_bitmaps();
  575. Unlock:
  576. unlock_device_hotplug();
  577. return error;
  578. }
  579. /**
  580. * hibernate - Carry out system hibernation, including saving the image.
  581. */
  582. int hibernate(void)
  583. {
  584. int error, nr_calls = 0;
  585. bool snapshot_test = false;
  586. if (!hibernation_available()) {
  587. pm_pr_dbg("Hibernation not available.\n");
  588. return -EPERM;
  589. }
  590. lock_system_sleep();
  591. /* The snapshot device should not be opened while we're running */
  592. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  593. error = -EBUSY;
  594. goto Unlock;
  595. }
  596. pr_info("hibernation entry\n");
  597. pm_prepare_console();
  598. error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
  599. if (error) {
  600. nr_calls--;
  601. goto Exit;
  602. }
  603. pr_info("Syncing filesystems ... \n");
  604. sys_sync();
  605. pr_info("done.\n");
  606. error = freeze_processes();
  607. if (error)
  608. goto Exit;
  609. lock_device_hotplug();
  610. /* Allocate memory management structures */
  611. error = create_basic_memory_bitmaps();
  612. if (error)
  613. goto Thaw;
  614. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  615. if (error || freezer_test_done)
  616. goto Free_bitmaps;
  617. if (in_suspend) {
  618. unsigned int flags = 0;
  619. if (hibernation_mode == HIBERNATION_PLATFORM)
  620. flags |= SF_PLATFORM_MODE;
  621. if (nocompress)
  622. flags |= SF_NOCOMPRESS_MODE;
  623. else
  624. flags |= SF_CRC32_MODE;
  625. pm_pr_dbg("Writing image.\n");
  626. error = swsusp_write(flags);
  627. swsusp_free();
  628. if (!error) {
  629. if (hibernation_mode == HIBERNATION_TEST_RESUME)
  630. snapshot_test = true;
  631. else
  632. power_down();
  633. }
  634. in_suspend = 0;
  635. pm_restore_gfp_mask();
  636. } else {
  637. pm_pr_dbg("Image restored successfully.\n");
  638. }
  639. Free_bitmaps:
  640. free_basic_memory_bitmaps();
  641. Thaw:
  642. unlock_device_hotplug();
  643. if (snapshot_test) {
  644. pm_pr_dbg("Checking hibernation image\n");
  645. error = swsusp_check();
  646. if (!error)
  647. error = load_image_and_restore();
  648. }
  649. thaw_processes();
  650. /* Don't bother checking whether freezer_test_done is true */
  651. freezer_test_done = false;
  652. Exit:
  653. __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
  654. pm_restore_console();
  655. atomic_inc(&snapshot_device_available);
  656. Unlock:
  657. unlock_system_sleep();
  658. pr_info("hibernation exit\n");
  659. return error;
  660. }
  661. /**
  662. * software_resume - Resume from a saved hibernation image.
  663. *
  664. * This routine is called as a late initcall, when all devices have been
  665. * discovered and initialized already.
  666. *
  667. * The image reading code is called to see if there is a hibernation image
  668. * available for reading. If that is the case, devices are quiesced and the
  669. * contents of memory is restored from the saved image.
  670. *
  671. * If this is successful, control reappears in the restored target kernel in
  672. * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
  673. * attempts to recover gracefully and make the kernel return to the normal mode
  674. * of operation.
  675. */
  676. static int software_resume(void)
  677. {
  678. int error, nr_calls = 0;
  679. /*
  680. * If the user said "noresume".. bail out early.
  681. */
  682. if (noresume || !hibernation_available())
  683. return 0;
  684. /*
  685. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  686. * is configured into the kernel. Since the regular hibernate
  687. * trigger path is via sysfs which takes a buffer mutex before
  688. * calling hibernate functions (which take pm_mutex) this can
  689. * cause lockdep to complain about a possible ABBA deadlock
  690. * which cannot happen since we're in the boot code here and
  691. * sysfs can't be invoked yet. Therefore, we use a subclass
  692. * here to avoid lockdep complaining.
  693. */
  694. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  695. if (swsusp_resume_device)
  696. goto Check_image;
  697. if (!strlen(resume_file)) {
  698. error = -ENOENT;
  699. goto Unlock;
  700. }
  701. pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
  702. if (resume_delay) {
  703. pr_info("Waiting %dsec before reading resume device ...\n",
  704. resume_delay);
  705. ssleep(resume_delay);
  706. }
  707. /* Check if the device is there */
  708. swsusp_resume_device = name_to_dev_t(resume_file);
  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. pm_pr_dbg("Hibernation image partition %d:%d present\n",
  728. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  729. pm_pr_dbg("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. pr_info("resume from hibernation\n");
  740. pm_prepare_console();
  741. error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
  742. if (error) {
  743. nr_calls--;
  744. goto Close_Finish;
  745. }
  746. pm_pr_dbg("Preparing processes for restore.\n");
  747. error = freeze_processes();
  748. if (error)
  749. goto Close_Finish;
  750. error = freeze_kernel_threads();
  751. if (error) {
  752. thaw_processes();
  753. goto Close_Finish;
  754. }
  755. error = load_image_and_restore();
  756. thaw_processes();
  757. Finish:
  758. __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
  759. pm_restore_console();
  760. pr_info("resume from hibernation failed (%d)\n", error);
  761. atomic_inc(&snapshot_device_available);
  762. /* For success case, the suspend path will release the lock */
  763. Unlock:
  764. mutex_unlock(&pm_mutex);
  765. pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
  766. return error;
  767. Close_Finish:
  768. swsusp_close(FMODE_READ);
  769. goto Finish;
  770. }
  771. late_initcall_sync(software_resume);
  772. static const char * const hibernation_modes[] = {
  773. [HIBERNATION_PLATFORM] = "platform",
  774. [HIBERNATION_SHUTDOWN] = "shutdown",
  775. [HIBERNATION_REBOOT] = "reboot",
  776. #ifdef CONFIG_SUSPEND
  777. [HIBERNATION_SUSPEND] = "suspend",
  778. #endif
  779. [HIBERNATION_TEST_RESUME] = "test_resume",
  780. };
  781. /*
  782. * /sys/power/disk - Control hibernation mode.
  783. *
  784. * Hibernation can be handled in several ways. There are a few different ways
  785. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  786. * or other hibernation_ops), powering it off or rebooting it (for testing
  787. * mostly).
  788. *
  789. * The sysfs file /sys/power/disk provides an interface for selecting the
  790. * hibernation mode to use. Reading from this file causes the available modes
  791. * to be printed. There are 3 modes that can be supported:
  792. *
  793. * 'platform'
  794. * 'shutdown'
  795. * 'reboot'
  796. *
  797. * If a platform hibernation driver is in use, 'platform' will be supported
  798. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  799. * The selected option (i.e. the one corresponding to the current value of
  800. * hibernation_mode) is enclosed by a square bracket.
  801. *
  802. * To select a given hibernation mode it is necessary to write the mode's
  803. * string representation (as returned by reading from /sys/power/disk) back
  804. * into /sys/power/disk.
  805. */
  806. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  807. char *buf)
  808. {
  809. int i;
  810. char *start = buf;
  811. if (!hibernation_available())
  812. return sprintf(buf, "[disabled]\n");
  813. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  814. if (!hibernation_modes[i])
  815. continue;
  816. switch (i) {
  817. case HIBERNATION_SHUTDOWN:
  818. case HIBERNATION_REBOOT:
  819. #ifdef CONFIG_SUSPEND
  820. case HIBERNATION_SUSPEND:
  821. #endif
  822. case HIBERNATION_TEST_RESUME:
  823. break;
  824. case HIBERNATION_PLATFORM:
  825. if (hibernation_ops)
  826. break;
  827. /* not a valid mode, continue with loop */
  828. continue;
  829. }
  830. if (i == hibernation_mode)
  831. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  832. else
  833. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  834. }
  835. buf += sprintf(buf, "\n");
  836. return buf-start;
  837. }
  838. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  839. const char *buf, size_t n)
  840. {
  841. int error = 0;
  842. int i;
  843. int len;
  844. char *p;
  845. int mode = HIBERNATION_INVALID;
  846. if (!hibernation_available())
  847. return -EPERM;
  848. p = memchr(buf, '\n', n);
  849. len = p ? p - buf : n;
  850. lock_system_sleep();
  851. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  852. if (len == strlen(hibernation_modes[i])
  853. && !strncmp(buf, hibernation_modes[i], len)) {
  854. mode = i;
  855. break;
  856. }
  857. }
  858. if (mode != HIBERNATION_INVALID) {
  859. switch (mode) {
  860. case HIBERNATION_SHUTDOWN:
  861. case HIBERNATION_REBOOT:
  862. #ifdef CONFIG_SUSPEND
  863. case HIBERNATION_SUSPEND:
  864. #endif
  865. case HIBERNATION_TEST_RESUME:
  866. hibernation_mode = mode;
  867. break;
  868. case HIBERNATION_PLATFORM:
  869. if (hibernation_ops)
  870. hibernation_mode = mode;
  871. else
  872. error = -EINVAL;
  873. }
  874. } else
  875. error = -EINVAL;
  876. if (!error)
  877. pm_pr_dbg("Hibernation mode set to '%s'\n",
  878. hibernation_modes[mode]);
  879. unlock_system_sleep();
  880. return error ? error : n;
  881. }
  882. power_attr(disk);
  883. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  884. char *buf)
  885. {
  886. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  887. MINOR(swsusp_resume_device));
  888. }
  889. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  890. const char *buf, size_t n)
  891. {
  892. dev_t res;
  893. int len = n;
  894. char *name;
  895. if (len && buf[len-1] == '\n')
  896. len--;
  897. name = kstrndup(buf, len, GFP_KERNEL);
  898. if (!name)
  899. return -ENOMEM;
  900. res = name_to_dev_t(name);
  901. kfree(name);
  902. if (!res)
  903. return -EINVAL;
  904. lock_system_sleep();
  905. swsusp_resume_device = res;
  906. unlock_system_sleep();
  907. pr_info("Starting manual resume from disk\n");
  908. noresume = 0;
  909. software_resume();
  910. return n;
  911. }
  912. power_attr(resume);
  913. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  914. char *buf)
  915. {
  916. return sprintf(buf, "%lu\n", image_size);
  917. }
  918. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  919. const char *buf, size_t n)
  920. {
  921. unsigned long size;
  922. if (sscanf(buf, "%lu", &size) == 1) {
  923. image_size = size;
  924. return n;
  925. }
  926. return -EINVAL;
  927. }
  928. power_attr(image_size);
  929. static ssize_t reserved_size_show(struct kobject *kobj,
  930. struct kobj_attribute *attr, char *buf)
  931. {
  932. return sprintf(buf, "%lu\n", reserved_size);
  933. }
  934. static ssize_t reserved_size_store(struct kobject *kobj,
  935. struct kobj_attribute *attr,
  936. const char *buf, size_t n)
  937. {
  938. unsigned long size;
  939. if (sscanf(buf, "%lu", &size) == 1) {
  940. reserved_size = size;
  941. return n;
  942. }
  943. return -EINVAL;
  944. }
  945. power_attr(reserved_size);
  946. static struct attribute * g[] = {
  947. &disk_attr.attr,
  948. &resume_attr.attr,
  949. &image_size_attr.attr,
  950. &reserved_size_attr.attr,
  951. NULL,
  952. };
  953. static const struct attribute_group attr_group = {
  954. .attrs = g,
  955. };
  956. static int __init pm_disk_init(void)
  957. {
  958. return sysfs_create_group(power_kobj, &attr_group);
  959. }
  960. core_initcall(pm_disk_init);
  961. static int __init resume_setup(char *str)
  962. {
  963. if (noresume)
  964. return 1;
  965. strncpy( resume_file, str, 255 );
  966. return 1;
  967. }
  968. static int __init resume_offset_setup(char *str)
  969. {
  970. unsigned long long offset;
  971. if (noresume)
  972. return 1;
  973. if (sscanf(str, "%llu", &offset) == 1)
  974. swsusp_resume_block = offset;
  975. return 1;
  976. }
  977. static int __init hibernate_setup(char *str)
  978. {
  979. if (!strncmp(str, "noresume", 8)) {
  980. noresume = 1;
  981. } else if (!strncmp(str, "nocompress", 10)) {
  982. nocompress = 1;
  983. } else if (!strncmp(str, "no", 2)) {
  984. noresume = 1;
  985. nohibernate = 1;
  986. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
  987. && !strncmp(str, "protect_image", 13)) {
  988. enable_restore_image_protection();
  989. }
  990. return 1;
  991. }
  992. static int __init noresume_setup(char *str)
  993. {
  994. noresume = 1;
  995. return 1;
  996. }
  997. static int __init resumewait_setup(char *str)
  998. {
  999. resume_wait = 1;
  1000. return 1;
  1001. }
  1002. static int __init resumedelay_setup(char *str)
  1003. {
  1004. int rc = kstrtouint(str, 0, &resume_delay);
  1005. if (rc)
  1006. return rc;
  1007. return 1;
  1008. }
  1009. static int __init nohibernate_setup(char *str)
  1010. {
  1011. noresume = 1;
  1012. nohibernate = 1;
  1013. return 1;
  1014. }
  1015. __setup("noresume", noresume_setup);
  1016. __setup("resume_offset=", resume_offset_setup);
  1017. __setup("resume=", resume_setup);
  1018. __setup("hibernate=", hibernate_setup);
  1019. __setup("resumewait", resumewait_setup);
  1020. __setup("resumedelay=", resumedelay_setup);
  1021. __setup("nohibernate", nohibernate_setup);