hibernate.c 27 KB

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