hibernate.c 28 KB

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