hibernate.c 28 KB

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