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