hibernate.c 25 KB

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