hibernate.c 27 KB

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