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