hibernate.c 26 KB

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