hibernate.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. __weak int arch_resume_nosmt(void)
  234. {
  235. return 0;
  236. }
  237. /**
  238. * create_image - Create a hibernation image.
  239. * @platform_mode: Whether or not to use the platform driver.
  240. *
  241. * Execute device drivers' "late" and "noirq" freeze callbacks, create a
  242. * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
  243. *
  244. * Control reappears in this routine after the subsequent restore.
  245. */
  246. static int create_image(int platform_mode)
  247. {
  248. int error;
  249. error = dpm_suspend_end(PMSG_FREEZE);
  250. if (error) {
  251. pr_err("Some devices failed to power down, aborting hibernation\n");
  252. return error;
  253. }
  254. error = platform_pre_snapshot(platform_mode);
  255. if (error || hibernation_test(TEST_PLATFORM))
  256. goto Platform_finish;
  257. error = disable_nonboot_cpus();
  258. if (error || hibernation_test(TEST_CPUS))
  259. goto Enable_cpus;
  260. local_irq_disable();
  261. system_state = SYSTEM_SUSPEND;
  262. error = syscore_suspend();
  263. if (error) {
  264. pr_err("Some system devices failed to power down, aborting hibernation\n");
  265. goto Enable_irqs;
  266. }
  267. if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
  268. goto Power_up;
  269. in_suspend = 1;
  270. save_processor_state();
  271. trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
  272. error = swsusp_arch_suspend();
  273. /* Restore control flow magically appears here */
  274. restore_processor_state();
  275. trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
  276. if (error)
  277. pr_err("Error %d creating hibernation image\n", error);
  278. if (!in_suspend) {
  279. events_check_enabled = false;
  280. clear_free_pages();
  281. }
  282. platform_leave(platform_mode);
  283. Power_up:
  284. syscore_resume();
  285. Enable_irqs:
  286. system_state = SYSTEM_RUNNING;
  287. local_irq_enable();
  288. Enable_cpus:
  289. enable_nonboot_cpus();
  290. /* Allow architectures to do nosmt-specific post-resume dances */
  291. if (!in_suspend)
  292. error = arch_resume_nosmt();
  293. Platform_finish:
  294. platform_finish(platform_mode);
  295. dpm_resume_start(in_suspend ?
  296. (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
  297. return error;
  298. }
  299. /**
  300. * hibernation_snapshot - Quiesce devices and create a hibernation image.
  301. * @platform_mode: If set, use platform driver to prepare for the transition.
  302. *
  303. * This routine must be called with system_transition_mutex held.
  304. */
  305. int hibernation_snapshot(int platform_mode)
  306. {
  307. pm_message_t msg;
  308. int error;
  309. pm_suspend_clear_flags();
  310. error = platform_begin(platform_mode);
  311. if (error)
  312. goto Close;
  313. /* Preallocate image memory before shutting down devices. */
  314. error = hibernate_preallocate_memory();
  315. if (error)
  316. goto Close;
  317. error = freeze_kernel_threads();
  318. if (error)
  319. goto Cleanup;
  320. if (hibernation_test(TEST_FREEZER)) {
  321. /*
  322. * Indicate to the caller that we are returning due to a
  323. * successful freezer test.
  324. */
  325. freezer_test_done = true;
  326. goto Thaw;
  327. }
  328. error = dpm_prepare(PMSG_FREEZE);
  329. if (error) {
  330. dpm_complete(PMSG_RECOVER);
  331. goto Thaw;
  332. }
  333. suspend_console();
  334. pm_restrict_gfp_mask();
  335. error = dpm_suspend(PMSG_FREEZE);
  336. if (error || hibernation_test(TEST_DEVICES))
  337. platform_recover(platform_mode);
  338. else
  339. error = create_image(platform_mode);
  340. /*
  341. * In the case that we call create_image() above, the control
  342. * returns here (1) after the image has been created or the
  343. * image creation has failed and (2) after a successful restore.
  344. */
  345. /* We may need to release the preallocated image pages here. */
  346. if (error || !in_suspend)
  347. swsusp_free();
  348. msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
  349. dpm_resume(msg);
  350. if (error || !in_suspend)
  351. pm_restore_gfp_mask();
  352. resume_console();
  353. dpm_complete(msg);
  354. Close:
  355. platform_end(platform_mode);
  356. return error;
  357. Thaw:
  358. thaw_kernel_threads();
  359. Cleanup:
  360. swsusp_free();
  361. goto Close;
  362. }
  363. int __weak hibernate_resume_nonboot_cpu_disable(void)
  364. {
  365. return disable_nonboot_cpus();
  366. }
  367. /**
  368. * resume_target_kernel - Restore system state from a hibernation image.
  369. * @platform_mode: Whether or not to use the platform driver.
  370. *
  371. * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
  372. * contents of highmem that have not been restored yet from the image and run
  373. * the low-level code that will restore the remaining contents of memory and
  374. * switch to the just restored target kernel.
  375. */
  376. static int resume_target_kernel(bool platform_mode)
  377. {
  378. int error;
  379. error = dpm_suspend_end(PMSG_QUIESCE);
  380. if (error) {
  381. pr_err("Some devices failed to power down, aborting resume\n");
  382. return error;
  383. }
  384. error = platform_pre_restore(platform_mode);
  385. if (error)
  386. goto Cleanup;
  387. error = hibernate_resume_nonboot_cpu_disable();
  388. if (error)
  389. goto Enable_cpus;
  390. local_irq_disable();
  391. system_state = SYSTEM_SUSPEND;
  392. error = syscore_suspend();
  393. if (error)
  394. goto Enable_irqs;
  395. save_processor_state();
  396. error = restore_highmem();
  397. if (!error) {
  398. error = swsusp_arch_resume();
  399. /*
  400. * The code below is only ever reached in case of a failure.
  401. * Otherwise, execution continues at the place where
  402. * swsusp_arch_suspend() was called.
  403. */
  404. BUG_ON(!error);
  405. /*
  406. * This call to restore_highmem() reverts the changes made by
  407. * the previous one.
  408. */
  409. restore_highmem();
  410. }
  411. /*
  412. * The only reason why swsusp_arch_resume() can fail is memory being
  413. * very tight, so we have to free it as soon as we can to avoid
  414. * subsequent failures.
  415. */
  416. swsusp_free();
  417. restore_processor_state();
  418. touch_softlockup_watchdog();
  419. syscore_resume();
  420. Enable_irqs:
  421. system_state = SYSTEM_RUNNING;
  422. local_irq_enable();
  423. Enable_cpus:
  424. enable_nonboot_cpus();
  425. Cleanup:
  426. platform_restore_cleanup(platform_mode);
  427. dpm_resume_start(PMSG_RECOVER);
  428. return error;
  429. }
  430. /**
  431. * hibernation_restore - Quiesce devices and restore from a hibernation image.
  432. * @platform_mode: If set, use platform driver to prepare for the transition.
  433. *
  434. * This routine must be called with system_transition_mutex held. If it is
  435. * successful, control reappears in the restored target kernel in
  436. * hibernation_snapshot().
  437. */
  438. int hibernation_restore(int platform_mode)
  439. {
  440. int error;
  441. pm_prepare_console();
  442. suspend_console();
  443. pm_restrict_gfp_mask();
  444. error = dpm_suspend_start(PMSG_QUIESCE);
  445. if (!error) {
  446. error = resume_target_kernel(platform_mode);
  447. /*
  448. * The above should either succeed and jump to the new kernel,
  449. * or return with an error. Otherwise things are just
  450. * undefined, so let's be paranoid.
  451. */
  452. BUG_ON(!error);
  453. }
  454. dpm_resume_end(PMSG_RECOVER);
  455. pm_restore_gfp_mask();
  456. resume_console();
  457. pm_restore_console();
  458. return error;
  459. }
  460. /**
  461. * hibernation_platform_enter - Power off the system using the platform driver.
  462. */
  463. int hibernation_platform_enter(void)
  464. {
  465. int error;
  466. if (!hibernation_ops)
  467. return -ENOSYS;
  468. /*
  469. * We have cancelled the power transition by running
  470. * hibernation_ops->finish() before saving the image, so we should let
  471. * the firmware know that we're going to enter the sleep state after all
  472. */
  473. error = hibernation_ops->begin();
  474. if (error)
  475. goto Close;
  476. entering_platform_hibernation = true;
  477. suspend_console();
  478. error = dpm_suspend_start(PMSG_HIBERNATE);
  479. if (error) {
  480. if (hibernation_ops->recover)
  481. hibernation_ops->recover();
  482. goto Resume_devices;
  483. }
  484. error = dpm_suspend_end(PMSG_HIBERNATE);
  485. if (error)
  486. goto Resume_devices;
  487. error = hibernation_ops->prepare();
  488. if (error)
  489. goto Platform_finish;
  490. error = disable_nonboot_cpus();
  491. if (error)
  492. goto Enable_cpus;
  493. local_irq_disable();
  494. system_state = SYSTEM_SUSPEND;
  495. syscore_suspend();
  496. if (pm_wakeup_pending()) {
  497. error = -EAGAIN;
  498. goto Power_up;
  499. }
  500. hibernation_ops->enter();
  501. /* We should never get here */
  502. while (1);
  503. Power_up:
  504. syscore_resume();
  505. system_state = SYSTEM_RUNNING;
  506. local_irq_enable();
  507. Enable_cpus:
  508. enable_nonboot_cpus();
  509. Platform_finish:
  510. hibernation_ops->finish();
  511. dpm_resume_start(PMSG_RESTORE);
  512. Resume_devices:
  513. entering_platform_hibernation = false;
  514. dpm_resume_end(PMSG_RESTORE);
  515. resume_console();
  516. Close:
  517. hibernation_ops->end();
  518. return error;
  519. }
  520. /**
  521. * power_down - Shut the machine down for hibernation.
  522. *
  523. * Use the platform driver, if configured, to put the system into the sleep
  524. * state corresponding to hibernation, or try to power it off or reboot,
  525. * depending on the value of hibernation_mode.
  526. */
  527. static void power_down(void)
  528. {
  529. #ifdef CONFIG_SUSPEND
  530. int error;
  531. if (hibernation_mode == HIBERNATION_SUSPEND) {
  532. error = suspend_devices_and_enter(PM_SUSPEND_MEM);
  533. if (error) {
  534. hibernation_mode = hibernation_ops ?
  535. HIBERNATION_PLATFORM :
  536. HIBERNATION_SHUTDOWN;
  537. } else {
  538. /* Restore swap signature. */
  539. error = swsusp_unmark();
  540. if (error)
  541. pr_err("Swap will be unusable! Try swapon -a.\n");
  542. return;
  543. }
  544. }
  545. #endif
  546. switch (hibernation_mode) {
  547. case HIBERNATION_REBOOT:
  548. kernel_restart(NULL);
  549. break;
  550. case HIBERNATION_PLATFORM:
  551. hibernation_platform_enter();
  552. /* Fall through */
  553. case HIBERNATION_SHUTDOWN:
  554. if (pm_power_off)
  555. kernel_power_off();
  556. break;
  557. }
  558. kernel_halt();
  559. /*
  560. * Valid image is on the disk, if we continue we risk serious data
  561. * corruption after resume.
  562. */
  563. pr_crit("Power down manually\n");
  564. while (1)
  565. cpu_relax();
  566. }
  567. static int load_image_and_restore(void)
  568. {
  569. int error;
  570. unsigned int flags;
  571. pm_pr_dbg("Loading hibernation image.\n");
  572. lock_device_hotplug();
  573. error = create_basic_memory_bitmaps();
  574. if (error)
  575. goto Unlock;
  576. error = swsusp_read(&flags);
  577. swsusp_close(FMODE_READ);
  578. if (!error)
  579. hibernation_restore(flags & SF_PLATFORM_MODE);
  580. pr_err("Failed to load hibernation image, recovering.\n");
  581. swsusp_free();
  582. free_basic_memory_bitmaps();
  583. Unlock:
  584. unlock_device_hotplug();
  585. return error;
  586. }
  587. /**
  588. * hibernate - Carry out system hibernation, including saving the image.
  589. */
  590. int hibernate(void)
  591. {
  592. int error, nr_calls = 0;
  593. bool snapshot_test = false;
  594. if (!hibernation_available()) {
  595. pm_pr_dbg("Hibernation not available.\n");
  596. return -EPERM;
  597. }
  598. lock_system_sleep();
  599. /* The snapshot device should not be opened while we're running */
  600. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  601. error = -EBUSY;
  602. goto Unlock;
  603. }
  604. pr_info("hibernation entry\n");
  605. pm_prepare_console();
  606. error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
  607. if (error) {
  608. nr_calls--;
  609. goto Exit;
  610. }
  611. pr_info("Syncing filesystems ... \n");
  612. ksys_sync();
  613. pr_info("done.\n");
  614. error = freeze_processes();
  615. if (error)
  616. goto Exit;
  617. lock_device_hotplug();
  618. /* Allocate memory management structures */
  619. error = create_basic_memory_bitmaps();
  620. if (error)
  621. goto Thaw;
  622. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  623. if (error || freezer_test_done)
  624. goto Free_bitmaps;
  625. if (in_suspend) {
  626. unsigned int flags = 0;
  627. if (hibernation_mode == HIBERNATION_PLATFORM)
  628. flags |= SF_PLATFORM_MODE;
  629. if (nocompress)
  630. flags |= SF_NOCOMPRESS_MODE;
  631. else
  632. flags |= SF_CRC32_MODE;
  633. pm_pr_dbg("Writing image.\n");
  634. error = swsusp_write(flags);
  635. swsusp_free();
  636. if (!error) {
  637. if (hibernation_mode == HIBERNATION_TEST_RESUME)
  638. snapshot_test = true;
  639. else
  640. power_down();
  641. }
  642. in_suspend = 0;
  643. pm_restore_gfp_mask();
  644. } else {
  645. pm_pr_dbg("Image restored successfully.\n");
  646. }
  647. Free_bitmaps:
  648. free_basic_memory_bitmaps();
  649. Thaw:
  650. unlock_device_hotplug();
  651. if (snapshot_test) {
  652. pm_pr_dbg("Checking hibernation image\n");
  653. error = swsusp_check();
  654. if (!error)
  655. error = load_image_and_restore();
  656. }
  657. thaw_processes();
  658. /* Don't bother checking whether freezer_test_done is true */
  659. freezer_test_done = false;
  660. Exit:
  661. __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
  662. pm_restore_console();
  663. atomic_inc(&snapshot_device_available);
  664. Unlock:
  665. unlock_system_sleep();
  666. pr_info("hibernation exit\n");
  667. return error;
  668. }
  669. /**
  670. * software_resume - Resume from a saved hibernation image.
  671. *
  672. * This routine is called as a late initcall, when all devices have been
  673. * discovered and initialized already.
  674. *
  675. * The image reading code is called to see if there is a hibernation image
  676. * available for reading. If that is the case, devices are quiesced and the
  677. * contents of memory is restored from the saved image.
  678. *
  679. * If this is successful, control reappears in the restored target kernel in
  680. * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
  681. * attempts to recover gracefully and make the kernel return to the normal mode
  682. * of operation.
  683. */
  684. static int software_resume(void)
  685. {
  686. int error, nr_calls = 0;
  687. /*
  688. * If the user said "noresume".. bail out early.
  689. */
  690. if (noresume || !hibernation_available())
  691. return 0;
  692. /*
  693. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  694. * is configured into the kernel. Since the regular hibernate
  695. * trigger path is via sysfs which takes a buffer mutex before
  696. * calling hibernate functions (which take system_transition_mutex)
  697. * this can cause lockdep to complain about a possible ABBA deadlock
  698. * which cannot happen since we're in the boot code here and
  699. * sysfs can't be invoked yet. Therefore, we use a subclass
  700. * here to avoid lockdep complaining.
  701. */
  702. mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
  703. if (swsusp_resume_device)
  704. goto Check_image;
  705. if (!strlen(resume_file)) {
  706. error = -ENOENT;
  707. goto Unlock;
  708. }
  709. pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
  710. if (resume_delay) {
  711. pr_info("Waiting %dsec before reading resume device ...\n",
  712. resume_delay);
  713. ssleep(resume_delay);
  714. }
  715. /* Check if the device is there */
  716. swsusp_resume_device = name_to_dev_t(resume_file);
  717. if (!swsusp_resume_device) {
  718. /*
  719. * Some device discovery might still be in progress; we need
  720. * to wait for this to finish.
  721. */
  722. wait_for_device_probe();
  723. if (resume_wait) {
  724. while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
  725. msleep(10);
  726. async_synchronize_full();
  727. }
  728. swsusp_resume_device = name_to_dev_t(resume_file);
  729. if (!swsusp_resume_device) {
  730. error = -ENODEV;
  731. goto Unlock;
  732. }
  733. }
  734. Check_image:
  735. pm_pr_dbg("Hibernation image partition %d:%d present\n",
  736. MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
  737. pm_pr_dbg("Looking for hibernation image.\n");
  738. error = swsusp_check();
  739. if (error)
  740. goto Unlock;
  741. /* The snapshot device should not be opened while we're running */
  742. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  743. error = -EBUSY;
  744. swsusp_close(FMODE_READ);
  745. goto Unlock;
  746. }
  747. pr_info("resume from hibernation\n");
  748. pm_prepare_console();
  749. error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
  750. if (error) {
  751. nr_calls--;
  752. goto Close_Finish;
  753. }
  754. pm_pr_dbg("Preparing processes for restore.\n");
  755. error = freeze_processes();
  756. if (error)
  757. goto Close_Finish;
  758. error = freeze_kernel_threads();
  759. if (error) {
  760. thaw_processes();
  761. goto Close_Finish;
  762. }
  763. error = load_image_and_restore();
  764. thaw_processes();
  765. Finish:
  766. __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
  767. pm_restore_console();
  768. pr_info("resume from hibernation failed (%d)\n", error);
  769. atomic_inc(&snapshot_device_available);
  770. /* For success case, the suspend path will release the lock */
  771. Unlock:
  772. mutex_unlock(&system_transition_mutex);
  773. pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
  774. return error;
  775. Close_Finish:
  776. swsusp_close(FMODE_READ);
  777. goto Finish;
  778. }
  779. late_initcall_sync(software_resume);
  780. static const char * const hibernation_modes[] = {
  781. [HIBERNATION_PLATFORM] = "platform",
  782. [HIBERNATION_SHUTDOWN] = "shutdown",
  783. [HIBERNATION_REBOOT] = "reboot",
  784. #ifdef CONFIG_SUSPEND
  785. [HIBERNATION_SUSPEND] = "suspend",
  786. #endif
  787. [HIBERNATION_TEST_RESUME] = "test_resume",
  788. };
  789. /*
  790. * /sys/power/disk - Control hibernation mode.
  791. *
  792. * Hibernation can be handled in several ways. There are a few different ways
  793. * to put the system into the sleep state: using the platform driver (e.g. ACPI
  794. * or other hibernation_ops), powering it off or rebooting it (for testing
  795. * mostly).
  796. *
  797. * The sysfs file /sys/power/disk provides an interface for selecting the
  798. * hibernation mode to use. Reading from this file causes the available modes
  799. * to be printed. There are 3 modes that can be supported:
  800. *
  801. * 'platform'
  802. * 'shutdown'
  803. * 'reboot'
  804. *
  805. * If a platform hibernation driver is in use, 'platform' will be supported
  806. * and will be used by default. Otherwise, 'shutdown' will be used by default.
  807. * The selected option (i.e. the one corresponding to the current value of
  808. * hibernation_mode) is enclosed by a square bracket.
  809. *
  810. * To select a given hibernation mode it is necessary to write the mode's
  811. * string representation (as returned by reading from /sys/power/disk) back
  812. * into /sys/power/disk.
  813. */
  814. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  815. char *buf)
  816. {
  817. int i;
  818. char *start = buf;
  819. if (!hibernation_available())
  820. return sprintf(buf, "[disabled]\n");
  821. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  822. if (!hibernation_modes[i])
  823. continue;
  824. switch (i) {
  825. case HIBERNATION_SHUTDOWN:
  826. case HIBERNATION_REBOOT:
  827. #ifdef CONFIG_SUSPEND
  828. case HIBERNATION_SUSPEND:
  829. #endif
  830. case HIBERNATION_TEST_RESUME:
  831. break;
  832. case HIBERNATION_PLATFORM:
  833. if (hibernation_ops)
  834. break;
  835. /* not a valid mode, continue with loop */
  836. continue;
  837. }
  838. if (i == hibernation_mode)
  839. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  840. else
  841. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  842. }
  843. buf += sprintf(buf, "\n");
  844. return buf-start;
  845. }
  846. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  847. const char *buf, size_t n)
  848. {
  849. int error = 0;
  850. int i;
  851. int len;
  852. char *p;
  853. int mode = HIBERNATION_INVALID;
  854. if (!hibernation_available())
  855. return -EPERM;
  856. p = memchr(buf, '\n', n);
  857. len = p ? p - buf : n;
  858. lock_system_sleep();
  859. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  860. if (len == strlen(hibernation_modes[i])
  861. && !strncmp(buf, hibernation_modes[i], len)) {
  862. mode = i;
  863. break;
  864. }
  865. }
  866. if (mode != HIBERNATION_INVALID) {
  867. switch (mode) {
  868. case HIBERNATION_SHUTDOWN:
  869. case HIBERNATION_REBOOT:
  870. #ifdef CONFIG_SUSPEND
  871. case HIBERNATION_SUSPEND:
  872. #endif
  873. case HIBERNATION_TEST_RESUME:
  874. hibernation_mode = mode;
  875. break;
  876. case HIBERNATION_PLATFORM:
  877. if (hibernation_ops)
  878. hibernation_mode = mode;
  879. else
  880. error = -EINVAL;
  881. }
  882. } else
  883. error = -EINVAL;
  884. if (!error)
  885. pm_pr_dbg("Hibernation mode set to '%s'\n",
  886. hibernation_modes[mode]);
  887. unlock_system_sleep();
  888. return error ? error : n;
  889. }
  890. power_attr(disk);
  891. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  892. char *buf)
  893. {
  894. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  895. MINOR(swsusp_resume_device));
  896. }
  897. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  898. const char *buf, size_t n)
  899. {
  900. dev_t res;
  901. int len = n;
  902. char *name;
  903. if (len && buf[len-1] == '\n')
  904. len--;
  905. name = kstrndup(buf, len, GFP_KERNEL);
  906. if (!name)
  907. return -ENOMEM;
  908. res = name_to_dev_t(name);
  909. kfree(name);
  910. if (!res)
  911. return -EINVAL;
  912. lock_system_sleep();
  913. swsusp_resume_device = res;
  914. unlock_system_sleep();
  915. pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device);
  916. noresume = 0;
  917. software_resume();
  918. return n;
  919. }
  920. power_attr(resume);
  921. static ssize_t resume_offset_show(struct kobject *kobj,
  922. struct kobj_attribute *attr, char *buf)
  923. {
  924. return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
  925. }
  926. static ssize_t resume_offset_store(struct kobject *kobj,
  927. struct kobj_attribute *attr, const char *buf,
  928. size_t n)
  929. {
  930. unsigned long long offset;
  931. int rc;
  932. rc = kstrtoull(buf, 0, &offset);
  933. if (rc)
  934. return rc;
  935. swsusp_resume_block = offset;
  936. return n;
  937. }
  938. power_attr(resume_offset);
  939. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  940. char *buf)
  941. {
  942. return sprintf(buf, "%lu\n", image_size);
  943. }
  944. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  945. const char *buf, size_t n)
  946. {
  947. unsigned long size;
  948. if (sscanf(buf, "%lu", &size) == 1) {
  949. image_size = size;
  950. return n;
  951. }
  952. return -EINVAL;
  953. }
  954. power_attr(image_size);
  955. static ssize_t reserved_size_show(struct kobject *kobj,
  956. struct kobj_attribute *attr, char *buf)
  957. {
  958. return sprintf(buf, "%lu\n", reserved_size);
  959. }
  960. static ssize_t reserved_size_store(struct kobject *kobj,
  961. struct kobj_attribute *attr,
  962. const char *buf, size_t n)
  963. {
  964. unsigned long size;
  965. if (sscanf(buf, "%lu", &size) == 1) {
  966. reserved_size = size;
  967. return n;
  968. }
  969. return -EINVAL;
  970. }
  971. power_attr(reserved_size);
  972. static struct attribute * g[] = {
  973. &disk_attr.attr,
  974. &resume_offset_attr.attr,
  975. &resume_attr.attr,
  976. &image_size_attr.attr,
  977. &reserved_size_attr.attr,
  978. NULL,
  979. };
  980. static const struct attribute_group attr_group = {
  981. .attrs = g,
  982. };
  983. static int __init pm_disk_init(void)
  984. {
  985. return sysfs_create_group(power_kobj, &attr_group);
  986. }
  987. core_initcall(pm_disk_init);
  988. static int __init resume_setup(char *str)
  989. {
  990. if (noresume)
  991. return 1;
  992. strncpy( resume_file, str, 255 );
  993. return 1;
  994. }
  995. static int __init resume_offset_setup(char *str)
  996. {
  997. unsigned long long offset;
  998. if (noresume)
  999. return 1;
  1000. if (sscanf(str, "%llu", &offset) == 1)
  1001. swsusp_resume_block = offset;
  1002. return 1;
  1003. }
  1004. static int __init hibernate_setup(char *str)
  1005. {
  1006. if (!strncmp(str, "noresume", 8)) {
  1007. noresume = 1;
  1008. } else if (!strncmp(str, "nocompress", 10)) {
  1009. nocompress = 1;
  1010. } else if (!strncmp(str, "no", 2)) {
  1011. noresume = 1;
  1012. nohibernate = 1;
  1013. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
  1014. && !strncmp(str, "protect_image", 13)) {
  1015. enable_restore_image_protection();
  1016. }
  1017. return 1;
  1018. }
  1019. static int __init noresume_setup(char *str)
  1020. {
  1021. noresume = 1;
  1022. return 1;
  1023. }
  1024. static int __init resumewait_setup(char *str)
  1025. {
  1026. resume_wait = 1;
  1027. return 1;
  1028. }
  1029. static int __init resumedelay_setup(char *str)
  1030. {
  1031. int rc = kstrtouint(str, 0, &resume_delay);
  1032. if (rc)
  1033. return rc;
  1034. return 1;
  1035. }
  1036. static int __init nohibernate_setup(char *str)
  1037. {
  1038. noresume = 1;
  1039. nohibernate = 1;
  1040. return 1;
  1041. }
  1042. __setup("noresume", noresume_setup);
  1043. __setup("resume_offset=", resume_offset_setup);
  1044. __setup("resume=", resume_setup);
  1045. __setup("hibernate=", hibernate_setup);
  1046. __setup("resumewait", resumewait_setup);
  1047. __setup("resumedelay=", resumedelay_setup);
  1048. __setup("nohibernate", nohibernate_setup);