hibernate.c 27 KB

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