hibernate.c 25 KB

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