hibernate.c 31 KB

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