hibernate.c 30 KB

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