hibernate.c 34 KB

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