hibernate.c 29 KB

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