hibernate.c 28 KB

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