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