hibernate.c 27 KB

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