hibernate.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
  8. * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
  9. *
  10. * This file is released under the GPLv2.
  11. */
  12. #define pr_fmt(fmt) "PM: " fmt
  13. #include <linux/export.h>
  14. #include <linux/suspend.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/reboot.h>
  17. #include <linux/string.h>
  18. #include <linux/device.h>
  19. #include <linux/async.h>
  20. #include <linux/delay.h>
  21. #include <linux/fs.h>
  22. #include <linux/mount.h>
  23. #include <linux/pm.h>
  24. #include <linux/nmi.h>
  25. #include <linux/console.h>
  26. #include <linux/cpu.h>
  27. #include <linux/freezer.h>
  28. #include <linux/gfp.h>
  29. #include <linux/syscore_ops.h>
  30. #include <linux/ctype.h>
  31. #include <linux/genhd.h>
  32. #include <linux/ktime.h>
  33. #include <trace/events/power.h>
  34. #include "power.h"
  35. static int nocompress;
  36. static int noresume;
  37. static int nohibernate;
  38. static int resume_wait;
  39. static unsigned int resume_delay;
  40. static char resume_file[256] = CONFIG_PM_STD_PARTITION;
  41. dev_t swsusp_resume_device;
  42. sector_t swsusp_resume_block;
  43. __visible int in_suspend __nosavedata;
  44. enum {
  45. HIBERNATION_INVALID,
  46. HIBERNATION_PLATFORM,
  47. HIBERNATION_SHUTDOWN,
  48. HIBERNATION_REBOOT,
  49. #ifdef CONFIG_SUSPEND
  50. HIBERNATION_SUSPEND,
  51. #endif
  52. HIBERNATION_TEST_RESUME,
  53. /* keep last */
  54. __HIBERNATION_AFTER_LAST
  55. };
  56. #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
  57. #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
  58. static int hibernation_mode = HIBERNATION_SHUTDOWN;
  59. bool freezer_test_done;
  60. static const struct platform_hibernation_ops *hibernation_ops;
  61. bool hibernation_available(void)
  62. {
  63. return (nohibernate == 0);
  64. }
  65. /**
  66. * hibernation_set_ops - Set the global hibernate operations.
  67. * @ops: Hibernation operations to use in subsequent hibernation transitions.
  68. */
  69. void hibernation_set_ops(const struct platform_hibernation_ops *ops)
  70. {
  71. if (ops && !(ops->begin && ops->end && ops->pre_snapshot
  72. && ops->prepare && ops->finish && ops->enter && ops->pre_restore
  73. && ops->restore_cleanup && ops->leave)) {
  74. WARN_ON(1);
  75. return;
  76. }
  77. lock_system_sleep();
  78. hibernation_ops = ops;
  79. if (ops)
  80. hibernation_mode = HIBERNATION_PLATFORM;
  81. else if (hibernation_mode == HIBERNATION_PLATFORM)
  82. hibernation_mode = HIBERNATION_SHUTDOWN;
  83. unlock_system_sleep();
  84. }
  85. EXPORT_SYMBOL_GPL(hibernation_set_ops);
  86. static bool entering_platform_hibernation;
  87. bool system_entering_hibernation(void)
  88. {
  89. return entering_platform_hibernation;
  90. }
  91. EXPORT_SYMBOL(system_entering_hibernation);
  92. #ifdef CONFIG_PM_DEBUG
  93. static void hibernation_debug_sleep(void)
  94. {
  95. pr_info("hibernation debug: Waiting for 5 seconds.\n");
  96. mdelay(5000);
  97. }
  98. static int hibernation_test(int level)
  99. {
  100. if (pm_test_level == level) {
  101. hibernation_debug_sleep();
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. #else /* !CONFIG_PM_DEBUG */
  107. static int hibernation_test(int level) { return 0; }
  108. #endif /* !CONFIG_PM_DEBUG */
  109. /**
  110. * platform_begin - Call platform to start hibernation.
  111. * @platform_mode: Whether or not to use the platform driver.
  112. */
  113. static int platform_begin(int platform_mode)
  114. {
  115. return (platform_mode && hibernation_ops) ?
  116. hibernation_ops->begin() : 0;
  117. }
  118. /**
  119. * platform_end - Call platform to finish transition to the working state.
  120. * @platform_mode: Whether or not to use the platform driver.
  121. */
  122. static void platform_end(int platform_mode)
  123. {
  124. if (platform_mode && hibernation_ops)
  125. hibernation_ops->end();
  126. }
  127. /**
  128. * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
  129. * @platform_mode: Whether or not to use the platform driver.
  130. *
  131. * Use the platform driver to prepare the system for creating a hibernate image,
  132. * if so configured, and return an error code if that fails.
  133. */
  134. static int platform_pre_snapshot(int platform_mode)
  135. {
  136. return (platform_mode && hibernation_ops) ?
  137. hibernation_ops->pre_snapshot() : 0;
  138. }
  139. /**
  140. * platform_leave - Call platform to prepare a transition to the working state.
  141. * @platform_mode: Whether or not to use the platform driver.
  142. *
  143. * Use the platform driver prepare to prepare the machine for switching to the
  144. * normal mode of operation.
  145. *
  146. * This routine is called on one CPU with interrupts disabled.
  147. */
  148. static void platform_leave(int platform_mode)
  149. {
  150. if (platform_mode && hibernation_ops)
  151. hibernation_ops->leave();
  152. }
  153. /**
  154. * platform_finish - Call platform to switch the system to the working state.
  155. * @platform_mode: Whether or not to use the platform driver.
  156. *
  157. * Use the platform driver to switch the machine to the normal mode of
  158. * operation.
  159. *
  160. * This routine must be called after platform_prepare().
  161. */
  162. static void platform_finish(int platform_mode)
  163. {
  164. if (platform_mode && hibernation_ops)
  165. hibernation_ops->finish();
  166. }
  167. /**
  168. * platform_pre_restore - Prepare for hibernate image restoration.
  169. * @platform_mode: Whether or not to use the platform driver.
  170. *
  171. * Use the platform driver to prepare the system for resume from a hibernation
  172. * image.
  173. *
  174. * If the restore fails after this function has been called,
  175. * platform_restore_cleanup() must be called.
  176. */
  177. static int platform_pre_restore(int platform_mode)
  178. {
  179. return (platform_mode && hibernation_ops) ?
  180. hibernation_ops->pre_restore() : 0;
  181. }
  182. /**
  183. * platform_restore_cleanup - Switch to the working state after failing restore.
  184. * @platform_mode: Whether or not to use the platform driver.
  185. *
  186. * Use the platform driver to switch the system to the normal mode of operation
  187. * after a failing restore.
  188. *
  189. * If platform_pre_restore() has been called before the failing restore, this
  190. * function must be called too, regardless of the result of
  191. * platform_pre_restore().
  192. */
  193. static void platform_restore_cleanup(int platform_mode)
  194. {
  195. if (platform_mode && hibernation_ops)
  196. hibernation_ops->restore_cleanup();
  197. }
  198. /**
  199. * platform_recover - Recover from a failure to suspend devices.
  200. * @platform_mode: Whether or not to use the platform driver.
  201. */
  202. static void platform_recover(int platform_mode)
  203. {
  204. if (platform_mode && hibernation_ops && hibernation_ops->recover)
  205. hibernation_ops->recover();
  206. }
  207. /**
  208. * swsusp_show_speed - Print time elapsed between two events during hibernation.
  209. * @start: Starting event.
  210. * @stop: Final event.
  211. * @nr_pages: Number of memory pages processed between @start and @stop.
  212. * @msg: Additional diagnostic message to print.
  213. */
  214. void swsusp_show_speed(ktime_t start, ktime_t stop,
  215. unsigned nr_pages, char *msg)
  216. {
  217. ktime_t diff;
  218. u64 elapsed_centisecs64;
  219. unsigned int centisecs;
  220. unsigned int k;
  221. unsigned int kps;
  222. diff = ktime_sub(stop, start);
  223. elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
  224. centisecs = elapsed_centisecs64;
  225. if (centisecs == 0)
  226. centisecs = 1; /* avoid div-by-zero */
  227. k = nr_pages * (PAGE_SIZE / 1024);
  228. kps = (k * 100) / centisecs;
  229. pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
  230. msg, k, centisecs / 100, centisecs % 100, kps / 1000,
  231. (kps % 1000) / 10);
  232. }
  233. /**
  234. * create_image - Create a hibernation image.
  235. * @platform_mode: Whether or not to use the platform driver.
  236. *
  237. * Execute device drivers' "late" and "noirq" freeze callbacks, create a
  238. * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
  239. *
  240. * Control reappears in this routine after the subsequent restore.
  241. */
  242. static int create_image(int platform_mode)
  243. {
  244. int error;
  245. error = dpm_suspend_end(PMSG_FREEZE);
  246. if (error) {
  247. pr_err("Some devices failed to power down, aborting hibernation\n");
  248. return error;
  249. }
  250. error = platform_pre_snapshot(platform_mode);
  251. if (error || hibernation_test(TEST_PLATFORM))
  252. goto Platform_finish;
  253. error = disable_nonboot_cpus();
  254. if (error || hibernation_test(TEST_CPUS))
  255. goto Enable_cpus;
  256. local_irq_disable();
  257. system_state = SYSTEM_SUSPEND;
  258. error = syscore_suspend();
  259. if (error) {
  260. pr_err("Some system devices failed to power down, 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. pr_err("Error %d creating hibernation image\n", error);
  274. if (!in_suspend) {
  275. events_check_enabled = false;
  276. clear_free_pages();
  277. }
  278. platform_leave(platform_mode);
  279. Power_up:
  280. syscore_resume();
  281. Enable_irqs:
  282. system_state = SYSTEM_RUNNING;
  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 system_transition_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. pr_err("Some devices failed to power down, aborting resume\n");
  375. return error;
  376. }
  377. error = platform_pre_restore(platform_mode);
  378. if (error)
  379. goto Cleanup;
  380. error = hibernate_resume_nonboot_cpu_disable();
  381. if (error)
  382. goto Enable_cpus;
  383. local_irq_disable();
  384. system_state = SYSTEM_SUSPEND;
  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. system_state = SYSTEM_RUNNING;
  415. local_irq_enable();
  416. Enable_cpus:
  417. enable_nonboot_cpus();
  418. Cleanup:
  419. platform_restore_cleanup(platform_mode);
  420. dpm_resume_start(PMSG_RECOVER);
  421. return error;
  422. }
  423. /**
  424. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  425. * @platform_mode: If set, use platform driver to prepare for the transition.
  426. *
  427. * This routine must be called with system_transition_mutex held. If it is
  428. * successful, control reappears in the restored target kernel in
  429. * hibernation_snapshot().
  430. */
  431. int hibernation_restore(int platform_mode)
  432. {
  433. int error;
  434. pm_prepare_console();
  435. suspend_console();
  436. pm_restrict_gfp_mask();
  437. error = dpm_suspend_start(PMSG_QUIESCE);
  438. if (!error) {
  439. error = resume_target_kernel(platform_mode);
  440. /*
  441. * The above should either succeed and jump to the new kernel,
  442. * or return with an error. Otherwise things are just
  443. * undefined, so let's be paranoid.
  444. */
  445. BUG_ON(!error);
  446. }
  447. dpm_resume_end(PMSG_RECOVER);
  448. pm_restore_gfp_mask();
  449. resume_console();
  450. pm_restore_console();
  451. return error;
  452. }
  453. /**
  454. * hibernation_platform_enter - Power off the system using the platform driver.
  455. */
  456. int hibernation_platform_enter(void)
  457. {
  458. int error;
  459. if (!hibernation_ops)
  460. return -ENOSYS;
  461. /*
  462. * We have cancelled the power transition by running
  463. * hibernation_ops->finish() before saving the image, so we should let
  464. * the firmware know that we're going to enter the sleep state after all
  465. */
  466. error = hibernation_ops->begin();
  467. if (error)
  468. goto Close;
  469. entering_platform_hibernation = true;
  470. suspend_console();
  471. error = dpm_suspend_start(PMSG_HIBERNATE);
  472. if (error) {
  473. if (hibernation_ops->recover)
  474. hibernation_ops->recover();
  475. goto Resume_devices;
  476. }
  477. error = dpm_suspend_end(PMSG_HIBERNATE);
  478. if (error)
  479. goto Resume_devices;
  480. error = hibernation_ops->prepare();
  481. if (error)
  482. goto Platform_finish;
  483. error = disable_nonboot_cpus();
  484. if (error)
  485. goto Enable_cpus;
  486. local_irq_disable();
  487. system_state = SYSTEM_SUSPEND;
  488. syscore_suspend();
  489. if (pm_wakeup_pending()) {
  490. error = -EAGAIN;
  491. goto Power_up;
  492. }
  493. hibernation_ops->enter();
  494. /* We should never get here */
  495. while (1);
  496. Power_up:
  497. syscore_resume();
  498. system_state = SYSTEM_RUNNING;
  499. local_irq_enable();
  500. Enable_cpus:
  501. enable_nonboot_cpus();
  502. Platform_finish:
  503. hibernation_ops->finish();
  504. dpm_resume_start(PMSG_RESTORE);
  505. Resume_devices:
  506. entering_platform_hibernation = false;
  507. dpm_resume_end(PMSG_RESTORE);
  508. resume_console();
  509. Close:
  510. hibernation_ops->end();
  511. return error;
  512. }
  513. /**
  514. * power_down - Shut the machine down for hibernation.
  515. *
  516. * Use the platform driver, if configured, to put the system into the sleep
  517. * state corresponding to hibernation, or try to power it off or reboot,
  518. * depending on the value of hibernation_mode.
  519. */
  520. static void power_down(void)
  521. {
  522. #ifdef CONFIG_SUSPEND
  523. int error;
  524. if (hibernation_mode == HIBERNATION_SUSPEND) {
  525. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  526. if (error) {
  527. hibernation_mode = hibernation_ops ?
  528. HIBERNATION_PLATFORM :
  529. HIBERNATION_SHUTDOWN;
  530. } else {
  531. /* Restore swap signature. */
  532. error = swsusp_unmark();
  533. if (error)
  534. pr_err("Swap will be unusable! Try swapon -a.\n");
  535. return;
  536. }
  537. }
  538. #endif
  539. switch (hibernation_mode) {
  540. case HIBERNATION_REBOOT:
  541. kernel_restart(NULL);
  542. break;
  543. case HIBERNATION_PLATFORM:
  544. hibernation_platform_enter();
  545. /* Fall through */
  546. case HIBERNATION_SHUTDOWN:
  547. if (pm_power_off)
  548. kernel_power_off();
  549. break;
  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. pr_crit("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. pm_pr_dbg("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. pr_err("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. pm_pr_dbg("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. pr_info("hibernation entry\n");
  598. pm_prepare_console();
  599. error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
  600. if (error) {
  601. nr_calls--;
  602. goto Exit;
  603. }
  604. pr_info("Syncing filesystems ... \n");
  605. ksys_sync();
  606. pr_info("done.\n");
  607. error = freeze_processes();
  608. if (error)
  609. goto Exit;
  610. lock_device_hotplug();
  611. /* Allocate memory management structures */
  612. error = create_basic_memory_bitmaps();
  613. if (error)
  614. goto Thaw;
  615. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  616. if (error || freezer_test_done)
  617. goto Free_bitmaps;
  618. if (in_suspend) {
  619. unsigned int flags = 0;
  620. if (hibernation_mode == HIBERNATION_PLATFORM)
  621. flags |= SF_PLATFORM_MODE;
  622. if (nocompress)
  623. flags |= SF_NOCOMPRESS_MODE;
  624. else
  625. flags |= SF_CRC32_MODE;
  626. pm_pr_dbg("Writing image.\n");
  627. error = swsusp_write(flags);
  628. swsusp_free();
  629. if (!error) {
  630. if (hibernation_mode == HIBERNATION_TEST_RESUME)
  631. snapshot_test = true;
  632. else
  633. power_down();
  634. }
  635. in_suspend = 0;
  636. pm_restore_gfp_mask();
  637. } else {
  638. pm_pr_dbg("Image restored successfully.\n");
  639. }
  640. Free_bitmaps:
  641. free_basic_memory_bitmaps();
  642. Thaw:
  643. unlock_device_hotplug();
  644. if (snapshot_test) {
  645. pm_pr_dbg("Checking hibernation image\n");
  646. error = swsusp_check();
  647. if (!error)
  648. error = load_image_and_restore();
  649. }
  650. thaw_processes();
  651. /* Don't bother checking whether freezer_test_done is true */
  652. freezer_test_done = false;
  653. Exit:
  654. __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
  655. pm_restore_console();
  656. atomic_inc(&snapshot_device_available);
  657. Unlock:
  658. unlock_system_sleep();
  659. pr_info("hibernation exit\n");
  660. return error;
  661. }
  662. /**
  663. * software_resume - Resume from a saved hibernation image.
  664. *
  665. * This routine is called as a late initcall, when all devices have been
  666. * discovered and initialized already.
  667. *
  668. * The image reading code is called to see if there is a hibernation image
  669. * available for reading. If that is the case, devices are quiesced and the
  670. * contents of memory is restored from the saved image.
  671. *
  672. * If this is successful, control reappears in the restored target kernel in
  673. * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
  674. * attempts to recover gracefully and make the kernel return to the normal mode
  675. * of operation.
  676. */
  677. static int software_resume(void)
  678. {
  679. int error, nr_calls = 0;
  680. /*
  681. * If the user said "noresume".. bail out early.
  682. */
  683. if (noresume || !hibernation_available())
  684. return 0;
  685. /*
  686. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  687. * is configured into the kernel. Since the regular hibernate
  688. * trigger path is via sysfs which takes a buffer mutex before
  689. * calling hibernate functions (which take system_transition_mutex)
  690. * this can cause lockdep to complain about a possible ABBA deadlock
  691. * which cannot happen since we're in the boot code here and
  692. * sysfs can't be invoked yet. Therefore, we use a subclass
  693. * here to avoid lockdep complaining.
  694. */
  695. mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
  696. if (swsusp_resume_device)
  697. goto Check_image;
  698. if (!strlen(resume_file)) {
  699. error = -ENOENT;
  700. goto Unlock;
  701. }
  702. pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
  703. if (resume_delay) {
  704. pr_info("Waiting %dsec before reading resume device ...\n",
  705. resume_delay);
  706. ssleep(resume_delay);
  707. }
  708. /* Check if the device is there */
  709. swsusp_resume_device = name_to_dev_t(resume_file);
  710. /*
  711. * name_to_dev_t is ineffective to verify parition if resume_file is in
  712. * integer format. (e.g. major:minor)
  713. */
  714. if (isdigit(resume_file[0]) && resume_wait) {
  715. int partno;
  716. while (!get_gendisk(swsusp_resume_device, &partno))
  717. msleep(10);
  718. }
  719. if (!swsusp_resume_device) {
  720. /*
  721. * Some device discovery might still be in progress; we need
  722. * to wait for this to finish.
  723. */
  724. wait_for_device_probe();
  725. if (resume_wait) {
  726. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  727. msleep(10);
  728. async_synchronize_full();
  729. }
  730. swsusp_resume_device = name_to_dev_t(resume_file);
  731. if (!swsusp_resume_device) {
  732. error = -ENODEV;
  733. goto Unlock;
  734. }
  735. }
  736. Check_image:
  737. pm_pr_dbg("Hibernation image partition %d:%d present\n",
  738. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  739. pm_pr_dbg("Looking for hibernation image.\n");
  740. error = swsusp_check();
  741. if (error)
  742. goto Unlock;
  743. /* The snapshot device should not be opened while we're running */
  744. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  745. error = -EBUSY;
  746. swsusp_close(FMODE_READ);
  747. goto Unlock;
  748. }
  749. pr_info("resume from hibernation\n");
  750. pm_prepare_console();
  751. error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
  752. if (error) {
  753. nr_calls--;
  754. goto Close_Finish;
  755. }
  756. pm_pr_dbg("Preparing processes for restore.\n");
  757. error = freeze_processes();
  758. if (error)
  759. goto Close_Finish;
  760. error = load_image_and_restore();
  761. thaw_processes();
  762. Finish:
  763. __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
  764. pm_restore_console();
  765. pr_info("resume from hibernation failed (%d)\n", error);
  766. atomic_inc(&snapshot_device_available);
  767. /* For success case, the suspend path will release the lock */
  768. Unlock:
  769. mutex_unlock(&system_transition_mutex);
  770. pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
  771. return error;
  772. Close_Finish:
  773. swsusp_close(FMODE_READ);
  774. goto Finish;
  775. }
  776. late_initcall_sync(software_resume);
  777. static const char * const hibernation_modes[] = {
  778. [HIBERNATION_PLATFORM] = "platform",
  779. [HIBERNATION_SHUTDOWN] = "shutdown",
  780. [HIBERNATION_REBOOT] = "reboot",
  781. #ifdef CONFIG_SUSPEND
  782. [HIBERNATION_SUSPEND] = "suspend",
  783. #endif
  784. [HIBERNATION_TEST_RESUME] = "test_resume",
  785. };
  786. /*
  787. * /sys/power/disk - Control hibernation mode.
  788. *
  789. * Hibernation can be handled in several ways. There are a few different ways
  790. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  791. * or other hibernation_ops), powering it off or rebooting it (for testing
  792. * mostly).
  793. *
  794. * The sysfs file /sys/power/disk provides an interface for selecting the
  795. * hibernation mode to use. Reading from this file causes the available modes
  796. * to be printed. There are 3 modes that can be supported:
  797. *
  798. * 'platform'
  799. * 'shutdown'
  800. * 'reboot'
  801. *
  802. * If a platform hibernation driver is in use, 'platform' will be supported
  803. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  804. * The selected option (i.e. the one corresponding to the current value of
  805. * hibernation_mode) is enclosed by a square bracket.
  806. *
  807. * To select a given hibernation mode it is necessary to write the mode's
  808. * string representation (as returned by reading from /sys/power/disk) back
  809. * into /sys/power/disk.
  810. */
  811. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  812. char *buf)
  813. {
  814. int i;
  815. char *start = buf;
  816. if (!hibernation_available())
  817. return sprintf(buf, "[disabled]\n");
  818. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  819. if (!hibernation_modes[i])
  820. continue;
  821. switch (i) {
  822. case HIBERNATION_SHUTDOWN:
  823. case HIBERNATION_REBOOT:
  824. #ifdef CONFIG_SUSPEND
  825. case HIBERNATION_SUSPEND:
  826. #endif
  827. case HIBERNATION_TEST_RESUME:
  828. break;
  829. case HIBERNATION_PLATFORM:
  830. if (hibernation_ops)
  831. break;
  832. /* not a valid mode, continue with loop */
  833. continue;
  834. }
  835. if (i == hibernation_mode)
  836. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  837. else
  838. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  839. }
  840. buf += sprintf(buf, "\n");
  841. return buf-start;
  842. }
  843. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  844. const char *buf, size_t n)
  845. {
  846. int error = 0;
  847. int i;
  848. int len;
  849. char *p;
  850. int mode = HIBERNATION_INVALID;
  851. if (!hibernation_available())
  852. return -EPERM;
  853. p = memchr(buf, '\n', n);
  854. len = p ? p - buf : n;
  855. lock_system_sleep();
  856. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  857. if (len == strlen(hibernation_modes[i])
  858. && !strncmp(buf, hibernation_modes[i], len)) {
  859. mode = i;
  860. break;
  861. }
  862. }
  863. if (mode != HIBERNATION_INVALID) {
  864. switch (mode) {
  865. case HIBERNATION_SHUTDOWN:
  866. case HIBERNATION_REBOOT:
  867. #ifdef CONFIG_SUSPEND
  868. case HIBERNATION_SUSPEND:
  869. #endif
  870. case HIBERNATION_TEST_RESUME:
  871. hibernation_mode = mode;
  872. break;
  873. case HIBERNATION_PLATFORM:
  874. if (hibernation_ops)
  875. hibernation_mode = mode;
  876. else
  877. error = -EINVAL;
  878. }
  879. } else
  880. error = -EINVAL;
  881. if (!error)
  882. pm_pr_dbg("Hibernation mode set to '%s'\n",
  883. hibernation_modes[mode]);
  884. unlock_system_sleep();
  885. return error ? error : n;
  886. }
  887. power_attr(disk);
  888. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  889. char *buf)
  890. {
  891. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  892. MINOR(swsusp_resume_device));
  893. }
  894. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  895. const char *buf, size_t n)
  896. {
  897. dev_t res;
  898. int len = n;
  899. char *name;
  900. if (len && buf[len-1] == '\n')
  901. len--;
  902. name = kstrndup(buf, len, GFP_KERNEL);
  903. if (!name)
  904. return -ENOMEM;
  905. res = name_to_dev_t(name);
  906. kfree(name);
  907. if (!res)
  908. return -EINVAL;
  909. lock_system_sleep();
  910. swsusp_resume_device = res;
  911. unlock_system_sleep();
  912. pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device);
  913. noresume = 0;
  914. software_resume();
  915. return n;
  916. }
  917. power_attr(resume);
  918. static ssize_t resume_offset_show(struct kobject *kobj,
  919. struct kobj_attribute *attr, char *buf)
  920. {
  921. return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
  922. }
  923. static ssize_t resume_offset_store(struct kobject *kobj,
  924. struct kobj_attribute *attr, const char *buf,
  925. size_t n)
  926. {
  927. unsigned long long offset;
  928. int rc;
  929. rc = kstrtoull(buf, 0, &offset);
  930. if (rc)
  931. return rc;
  932. swsusp_resume_block = offset;
  933. return n;
  934. }
  935. power_attr(resume_offset);
  936. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  937. char *buf)
  938. {
  939. return sprintf(buf, "%lu\n", image_size);
  940. }
  941. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  942. const char *buf, size_t n)
  943. {
  944. unsigned long size;
  945. if (sscanf(buf, "%lu", &size) == 1) {
  946. image_size = size;
  947. return n;
  948. }
  949. return -EINVAL;
  950. }
  951. power_attr(image_size);
  952. static ssize_t reserved_size_show(struct kobject *kobj,
  953. struct kobj_attribute *attr, char *buf)
  954. {
  955. return sprintf(buf, "%lu\n", reserved_size);
  956. }
  957. static ssize_t reserved_size_store(struct kobject *kobj,
  958. struct kobj_attribute *attr,
  959. const char *buf, size_t n)
  960. {
  961. unsigned long size;
  962. if (sscanf(buf, "%lu", &size) == 1) {
  963. reserved_size = size;
  964. return n;
  965. }
  966. return -EINVAL;
  967. }
  968. power_attr(reserved_size);
  969. static struct attribute * g[] = {
  970. &disk_attr.attr,
  971. &resume_offset_attr.attr,
  972. &resume_attr.attr,
  973. &image_size_attr.attr,
  974. &reserved_size_attr.attr,
  975. NULL,
  976. };
  977. static const struct attribute_group attr_group = {
  978. .attrs = g,
  979. };
  980. static int __init pm_disk_init(void)
  981. {
  982. return sysfs_create_group(power_kobj, &attr_group);
  983. }
  984. core_initcall(pm_disk_init);
  985. static int __init resume_setup(char *str)
  986. {
  987. if (noresume)
  988. return 1;
  989. strncpy( resume_file, str, 255 );
  990. return 1;
  991. }
  992. static int __init resume_offset_setup(char *str)
  993. {
  994. unsigned long long offset;
  995. if (noresume)
  996. return 1;
  997. if (sscanf(str, "%llu", &offset) == 1)
  998. swsusp_resume_block = offset;
  999. return 1;
  1000. }
  1001. static int __init hibernate_setup(char *str)
  1002. {
  1003. if (!strncmp(str, "noresume", 8)) {
  1004. noresume = 1;
  1005. } else if (!strncmp(str, "nocompress", 10)) {
  1006. nocompress = 1;
  1007. } else if (!strncmp(str, "no", 2)) {
  1008. noresume = 1;
  1009. nohibernate = 1;
  1010. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
  1011. && !strncmp(str, "protect_image", 13)) {
  1012. enable_restore_image_protection();
  1013. }
  1014. return 1;
  1015. }
  1016. static int __init noresume_setup(char *str)
  1017. {
  1018. noresume = 1;
  1019. return 1;
  1020. }
  1021. static int __init resumewait_setup(char *str)
  1022. {
  1023. resume_wait = 1;
  1024. return 1;
  1025. }
  1026. static int __init resumedelay_setup(char *str)
  1027. {
  1028. int rc = kstrtouint(str, 0, &resume_delay);
  1029. if (rc)
  1030. return rc;
  1031. return 1;
  1032. }
  1033. static int __init nohibernate_setup(char *str)
  1034. {
  1035. noresume = 1;
  1036. nohibernate = 1;
  1037. return 1;
  1038. }
  1039. __setup("noresume", noresume_setup);
  1040. __setup("resume_offset=", resume_offset_setup);
  1041. __setup("resume=", resume_setup);
  1042. __setup("hibernate=", hibernate_setup);
  1043. __setup("resumewait", resumewait_setup);
  1044. __setup("resumedelay=", resumedelay_setup);
  1045. __setup("nohibernate", nohibernate_setup);