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 
hibernate_acquire(void)72 bool hibernate_acquire(void)
73 {
74 	return atomic_add_unless(&hibernate_atomic, -1, 0);
75 }
76 
hibernate_release(void)77 void hibernate_release(void)
78 {
79 	atomic_inc(&hibernate_atomic);
80 }
81 
hibernation_available(void)82 bool hibernation_available(void)
83 {
84 	return nohibernate == 0 &&
85 		!security_locked_down(LOCKDOWN_HIBERNATION) &&
86 		!secretmem_active() && !cxl_mem_active();
87 }
88 
89 /**
90  * hibernation_set_ops - Set the global hibernate operations.
91  * @ops: Hibernation operations to use in subsequent hibernation transitions.
92  */
hibernation_set_ops(const struct platform_hibernation_ops * ops)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 
system_entering_hibernation(void)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
hibernation_debug_sleep(void)121 static void hibernation_debug_sleep(void)
122 {
123 	pr_info("debug: Waiting for 5 seconds.\n");
124 	mdelay(5000);
125 }
126 
hibernation_test(int level)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 */
hibernation_test(int level)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  */
platform_begin(int platform_mode)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  */
platform_end(int platform_mode)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 
platform_pre_snapshot(int platform_mode)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  */
platform_leave(int platform_mode)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  */
platform_finish(int platform_mode)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  */
platform_pre_restore(int platform_mode)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  */
platform_restore_cleanup(int platform_mode)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  */
platform_recover(int platform_mode)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  */
swsusp_show_speed(ktime_t start,ktime_t stop,unsigned nr_pages,char * msg)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 
arch_resume_nosmt(void)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  */
create_image(int platform_mode)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  */
hibernation_snapshot(int platform_mode)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 
hibernate_resume_nonboot_cpu_disable(void)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  */
resume_target_kernel(bool platform_mode)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  */
hibernation_restore(int platform_mode)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  */
hibernation_platform_enter(void)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  */
power_down(void)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 (kernel_can_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 
load_image_and_restore(void)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 		swsusp_close(FMODE_READ | FMODE_EXCL);
693 		goto Unlock;
694 	}
695 
696 	error = swsusp_read(&flags);
697 	swsusp_close(FMODE_READ | FMODE_EXCL);
698 	if (!error)
699 		error = hibernation_restore(flags & SF_PLATFORM_MODE);
700 
701 	pr_err("Failed to load image, recovering.\n");
702 	swsusp_free();
703 	free_basic_memory_bitmaps();
704  Unlock:
705 	unlock_device_hotplug();
706 
707 	return error;
708 }
709 
710 /**
711  * hibernate - Carry out system hibernation, including saving the image.
712  */
hibernate(void)713 int hibernate(void)
714 {
715 	bool snapshot_test = false;
716 	int error;
717 
718 	if (!hibernation_available()) {
719 		pm_pr_dbg("Hibernation not available.\n");
720 		return -EPERM;
721 	}
722 
723 	lock_system_sleep();
724 	/* The snapshot device should not be opened while we're running */
725 	if (!hibernate_acquire()) {
726 		error = -EBUSY;
727 		goto Unlock;
728 	}
729 
730 	pr_info("hibernation entry\n");
731 	pm_prepare_console();
732 	error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
733 	if (error)
734 		goto Restore;
735 
736 	ksys_sync_helper();
737 
738 	error = freeze_processes();
739 	if (error)
740 		goto Exit;
741 
742 	lock_device_hotplug();
743 	/* Allocate memory management structures */
744 	error = create_basic_memory_bitmaps();
745 	if (error)
746 		goto Thaw;
747 
748 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
749 	if (error || freezer_test_done)
750 		goto Free_bitmaps;
751 
752 	if (in_suspend) {
753 		unsigned int flags = 0;
754 
755 		if (hibernation_mode == HIBERNATION_PLATFORM)
756 			flags |= SF_PLATFORM_MODE;
757 		if (nocompress)
758 			flags |= SF_NOCOMPRESS_MODE;
759 		else
760 		        flags |= SF_CRC32_MODE;
761 
762 		pm_pr_dbg("Writing hibernation image.\n");
763 		error = swsusp_write(flags);
764 		swsusp_free();
765 		if (!error) {
766 			if (hibernation_mode == HIBERNATION_TEST_RESUME)
767 				snapshot_test = true;
768 			else
769 				power_down();
770 		}
771 		in_suspend = 0;
772 		pm_restore_gfp_mask();
773 	} else {
774 		pm_pr_dbg("Hibernation image restored successfully.\n");
775 	}
776 
777  Free_bitmaps:
778 	free_basic_memory_bitmaps();
779  Thaw:
780 	unlock_device_hotplug();
781 	if (snapshot_test) {
782 		pm_pr_dbg("Checking hibernation image\n");
783 		error = swsusp_check();
784 		if (!error)
785 			error = load_image_and_restore();
786 	}
787 	thaw_processes();
788 
789 	/* Don't bother checking whether freezer_test_done is true */
790 	freezer_test_done = false;
791  Exit:
792 	pm_notifier_call_chain(PM_POST_HIBERNATION);
793  Restore:
794 	pm_restore_console();
795 	hibernate_release();
796  Unlock:
797 	unlock_system_sleep();
798 	pr_info("hibernation exit\n");
799 
800 	return error;
801 }
802 
803 /**
804  * hibernate_quiet_exec - Execute a function with all devices frozen.
805  * @func: Function to execute.
806  * @data: Data pointer to pass to @func.
807  *
808  * Return the @func return value or an error code if it cannot be executed.
809  */
hibernate_quiet_exec(int (* func)(void * data),void * data)810 int hibernate_quiet_exec(int (*func)(void *data), void *data)
811 {
812 	int error;
813 
814 	lock_system_sleep();
815 
816 	if (!hibernate_acquire()) {
817 		error = -EBUSY;
818 		goto unlock;
819 	}
820 
821 	pm_prepare_console();
822 
823 	error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
824 	if (error)
825 		goto restore;
826 
827 	error = freeze_processes();
828 	if (error)
829 		goto exit;
830 
831 	lock_device_hotplug();
832 
833 	pm_suspend_clear_flags();
834 
835 	error = platform_begin(true);
836 	if (error)
837 		goto thaw;
838 
839 	error = freeze_kernel_threads();
840 	if (error)
841 		goto thaw;
842 
843 	error = dpm_prepare(PMSG_FREEZE);
844 	if (error)
845 		goto dpm_complete;
846 
847 	suspend_console();
848 
849 	error = dpm_suspend(PMSG_FREEZE);
850 	if (error)
851 		goto dpm_resume;
852 
853 	error = dpm_suspend_end(PMSG_FREEZE);
854 	if (error)
855 		goto dpm_resume;
856 
857 	error = platform_pre_snapshot(true);
858 	if (error)
859 		goto skip;
860 
861 	error = func(data);
862 
863 skip:
864 	platform_finish(true);
865 
866 	dpm_resume_start(PMSG_THAW);
867 
868 dpm_resume:
869 	dpm_resume(PMSG_THAW);
870 
871 	resume_console();
872 
873 dpm_complete:
874 	dpm_complete(PMSG_THAW);
875 
876 	thaw_kernel_threads();
877 
878 thaw:
879 	platform_end(true);
880 
881 	unlock_device_hotplug();
882 
883 	thaw_processes();
884 
885 exit:
886 	pm_notifier_call_chain(PM_POST_HIBERNATION);
887 
888 restore:
889 	pm_restore_console();
890 
891 	hibernate_release();
892 
893 unlock:
894 	unlock_system_sleep();
895 
896 	return error;
897 }
898 EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
899 
900 /**
901  * software_resume - Resume from a saved hibernation image.
902  *
903  * This routine is called as a late initcall, when all devices have been
904  * discovered and initialized already.
905  *
906  * The image reading code is called to see if there is a hibernation image
907  * available for reading.  If that is the case, devices are quiesced and the
908  * contents of memory is restored from the saved image.
909  *
910  * If this is successful, control reappears in the restored target kernel in
911  * hibernation_snapshot() which returns to hibernate().  Otherwise, the routine
912  * attempts to recover gracefully and make the kernel return to the normal mode
913  * of operation.
914  */
software_resume(void)915 static int software_resume(void)
916 {
917 	int error;
918 
919 	/*
920 	 * If the user said "noresume".. bail out early.
921 	 */
922 	if (noresume || !hibernation_available())
923 		return 0;
924 
925 	/*
926 	 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
927 	 * is configured into the kernel. Since the regular hibernate
928 	 * trigger path is via sysfs which takes a buffer mutex before
929 	 * calling hibernate functions (which take system_transition_mutex)
930 	 * this can cause lockdep to complain about a possible ABBA deadlock
931 	 * which cannot happen since we're in the boot code here and
932 	 * sysfs can't be invoked yet. Therefore, we use a subclass
933 	 * here to avoid lockdep complaining.
934 	 */
935 	mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
936 
937 	if (swsusp_resume_device)
938 		goto Check_image;
939 
940 	if (!strlen(resume_file)) {
941 		error = -ENOENT;
942 		goto Unlock;
943 	}
944 
945 	pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
946 
947 	if (resume_delay) {
948 		pr_info("Waiting %dsec before reading resume device ...\n",
949 			resume_delay);
950 		ssleep(resume_delay);
951 	}
952 
953 	/* Check if the device is there */
954 	swsusp_resume_device = name_to_dev_t(resume_file);
955 	if (!swsusp_resume_device) {
956 		/*
957 		 * Some device discovery might still be in progress; we need
958 		 * to wait for this to finish.
959 		 */
960 		wait_for_device_probe();
961 
962 		if (resume_wait) {
963 			while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
964 				msleep(10);
965 			async_synchronize_full();
966 		}
967 
968 		swsusp_resume_device = name_to_dev_t(resume_file);
969 		if (!swsusp_resume_device) {
970 			error = -ENODEV;
971 			goto Unlock;
972 		}
973 	}
974 
975  Check_image:
976 	pm_pr_dbg("Hibernation image partition %d:%d present\n",
977 		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
978 
979 	pm_pr_dbg("Looking for hibernation image.\n");
980 	error = swsusp_check();
981 	if (error)
982 		goto Unlock;
983 
984 	/* The snapshot device should not be opened while we're running */
985 	if (!hibernate_acquire()) {
986 		error = -EBUSY;
987 		swsusp_close(FMODE_READ | FMODE_EXCL);
988 		goto Unlock;
989 	}
990 
991 	pr_info("resume from hibernation\n");
992 	pm_prepare_console();
993 	error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
994 	if (error)
995 		goto Restore;
996 
997 	pm_pr_dbg("Preparing processes for hibernation restore.\n");
998 	error = freeze_processes();
999 	if (error)
1000 		goto Close_Finish;
1001 
1002 	error = freeze_kernel_threads();
1003 	if (error) {
1004 		thaw_processes();
1005 		goto Close_Finish;
1006 	}
1007 
1008 	error = load_image_and_restore();
1009 	thaw_processes();
1010  Finish:
1011 	pm_notifier_call_chain(PM_POST_RESTORE);
1012  Restore:
1013 	pm_restore_console();
1014 	pr_info("resume failed (%d)\n", error);
1015 	hibernate_release();
1016 	/* For success case, the suspend path will release the lock */
1017  Unlock:
1018 	mutex_unlock(&system_transition_mutex);
1019 	pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
1020 	return error;
1021  Close_Finish:
1022 	swsusp_close(FMODE_READ | FMODE_EXCL);
1023 	goto Finish;
1024 }
1025 
1026 late_initcall_sync(software_resume);
1027 
1028 
1029 static const char * const hibernation_modes[] = {
1030 	[HIBERNATION_PLATFORM]	= "platform",
1031 	[HIBERNATION_SHUTDOWN]	= "shutdown",
1032 	[HIBERNATION_REBOOT]	= "reboot",
1033 #ifdef CONFIG_SUSPEND
1034 	[HIBERNATION_SUSPEND]	= "suspend",
1035 #endif
1036 	[HIBERNATION_TEST_RESUME]	= "test_resume",
1037 };
1038 
1039 /*
1040  * /sys/power/disk - Control hibernation mode.
1041  *
1042  * Hibernation can be handled in several ways.  There are a few different ways
1043  * to put the system into the sleep state: using the platform driver (e.g. ACPI
1044  * or other hibernation_ops), powering it off or rebooting it (for testing
1045  * mostly).
1046  *
1047  * The sysfs file /sys/power/disk provides an interface for selecting the
1048  * hibernation mode to use.  Reading from this file causes the available modes
1049  * to be printed.  There are 3 modes that can be supported:
1050  *
1051  *	'platform'
1052  *	'shutdown'
1053  *	'reboot'
1054  *
1055  * If a platform hibernation driver is in use, 'platform' will be supported
1056  * and will be used by default.  Otherwise, 'shutdown' will be used by default.
1057  * The selected option (i.e. the one corresponding to the current value of
1058  * hibernation_mode) is enclosed by a square bracket.
1059  *
1060  * To select a given hibernation mode it is necessary to write the mode's
1061  * string representation (as returned by reading from /sys/power/disk) back
1062  * into /sys/power/disk.
1063  */
1064 
disk_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1065 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1066 			 char *buf)
1067 {
1068 	int i;
1069 	char *start = buf;
1070 
1071 	if (!hibernation_available())
1072 		return sprintf(buf, "[disabled]\n");
1073 
1074 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1075 		if (!hibernation_modes[i])
1076 			continue;
1077 		switch (i) {
1078 		case HIBERNATION_SHUTDOWN:
1079 		case HIBERNATION_REBOOT:
1080 #ifdef CONFIG_SUSPEND
1081 		case HIBERNATION_SUSPEND:
1082 #endif
1083 		case HIBERNATION_TEST_RESUME:
1084 			break;
1085 		case HIBERNATION_PLATFORM:
1086 			if (hibernation_ops)
1087 				break;
1088 			/* not a valid mode, continue with loop */
1089 			continue;
1090 		}
1091 		if (i == hibernation_mode)
1092 			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
1093 		else
1094 			buf += sprintf(buf, "%s ", hibernation_modes[i]);
1095 	}
1096 	buf += sprintf(buf, "\n");
1097 	return buf-start;
1098 }
1099 
disk_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)1100 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1101 			  const char *buf, size_t n)
1102 {
1103 	int error = 0;
1104 	int i;
1105 	int len;
1106 	char *p;
1107 	int mode = HIBERNATION_INVALID;
1108 
1109 	if (!hibernation_available())
1110 		return -EPERM;
1111 
1112 	p = memchr(buf, '\n', n);
1113 	len = p ? p - buf : n;
1114 
1115 	lock_system_sleep();
1116 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1117 		if (len == strlen(hibernation_modes[i])
1118 		    && !strncmp(buf, hibernation_modes[i], len)) {
1119 			mode = i;
1120 			break;
1121 		}
1122 	}
1123 	if (mode != HIBERNATION_INVALID) {
1124 		switch (mode) {
1125 		case HIBERNATION_SHUTDOWN:
1126 		case HIBERNATION_REBOOT:
1127 #ifdef CONFIG_SUSPEND
1128 		case HIBERNATION_SUSPEND:
1129 #endif
1130 		case HIBERNATION_TEST_RESUME:
1131 			hibernation_mode = mode;
1132 			break;
1133 		case HIBERNATION_PLATFORM:
1134 			if (hibernation_ops)
1135 				hibernation_mode = mode;
1136 			else
1137 				error = -EINVAL;
1138 		}
1139 	} else
1140 		error = -EINVAL;
1141 
1142 	if (!error)
1143 		pm_pr_dbg("Hibernation mode set to '%s'\n",
1144 			       hibernation_modes[mode]);
1145 	unlock_system_sleep();
1146 	return error ? error : n;
1147 }
1148 
1149 power_attr(disk);
1150 
resume_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1151 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1152 			   char *buf)
1153 {
1154 	return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
1155 		       MINOR(swsusp_resume_device));
1156 }
1157 
resume_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)1158 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1159 			    const char *buf, size_t n)
1160 {
1161 	dev_t res;
1162 	int len = n;
1163 	char *name;
1164 
1165 	if (len && buf[len-1] == '\n')
1166 		len--;
1167 	name = kstrndup(buf, len, GFP_KERNEL);
1168 	if (!name)
1169 		return -ENOMEM;
1170 
1171 	res = name_to_dev_t(name);
1172 	kfree(name);
1173 	if (!res)
1174 		return -EINVAL;
1175 
1176 	lock_system_sleep();
1177 	swsusp_resume_device = res;
1178 	unlock_system_sleep();
1179 	pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1180 		  swsusp_resume_device);
1181 	noresume = 0;
1182 	software_resume();
1183 	return n;
1184 }
1185 
1186 power_attr(resume);
1187 
resume_offset_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1188 static ssize_t resume_offset_show(struct kobject *kobj,
1189 				  struct kobj_attribute *attr, char *buf)
1190 {
1191 	return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1192 }
1193 
resume_offset_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)1194 static ssize_t resume_offset_store(struct kobject *kobj,
1195 				   struct kobj_attribute *attr, const char *buf,
1196 				   size_t n)
1197 {
1198 	unsigned long long offset;
1199 	int rc;
1200 
1201 	rc = kstrtoull(buf, 0, &offset);
1202 	if (rc)
1203 		return rc;
1204 	swsusp_resume_block = offset;
1205 
1206 	return n;
1207 }
1208 
1209 power_attr(resume_offset);
1210 
image_size_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1211 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1212 			       char *buf)
1213 {
1214 	return sprintf(buf, "%lu\n", image_size);
1215 }
1216 
image_size_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)1217 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1218 				const char *buf, size_t n)
1219 {
1220 	unsigned long size;
1221 
1222 	if (sscanf(buf, "%lu", &size) == 1) {
1223 		image_size = size;
1224 		return n;
1225 	}
1226 
1227 	return -EINVAL;
1228 }
1229 
1230 power_attr(image_size);
1231 
reserved_size_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1232 static ssize_t reserved_size_show(struct kobject *kobj,
1233 				  struct kobj_attribute *attr, char *buf)
1234 {
1235 	return sprintf(buf, "%lu\n", reserved_size);
1236 }
1237 
reserved_size_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)1238 static ssize_t reserved_size_store(struct kobject *kobj,
1239 				   struct kobj_attribute *attr,
1240 				   const char *buf, size_t n)
1241 {
1242 	unsigned long size;
1243 
1244 	if (sscanf(buf, "%lu", &size) == 1) {
1245 		reserved_size = size;
1246 		return n;
1247 	}
1248 
1249 	return -EINVAL;
1250 }
1251 
1252 power_attr(reserved_size);
1253 
1254 static struct attribute *g[] = {
1255 	&disk_attr.attr,
1256 	&resume_offset_attr.attr,
1257 	&resume_attr.attr,
1258 	&image_size_attr.attr,
1259 	&reserved_size_attr.attr,
1260 	NULL,
1261 };
1262 
1263 
1264 static const struct attribute_group attr_group = {
1265 	.attrs = g,
1266 };
1267 
1268 
pm_disk_init(void)1269 static int __init pm_disk_init(void)
1270 {
1271 	return sysfs_create_group(power_kobj, &attr_group);
1272 }
1273 
1274 core_initcall(pm_disk_init);
1275 
1276 
resume_setup(char * str)1277 static int __init resume_setup(char *str)
1278 {
1279 	if (noresume)
1280 		return 1;
1281 
1282 	strncpy(resume_file, str, 255);
1283 	return 1;
1284 }
1285 
resume_offset_setup(char * str)1286 static int __init resume_offset_setup(char *str)
1287 {
1288 	unsigned long long offset;
1289 
1290 	if (noresume)
1291 		return 1;
1292 
1293 	if (sscanf(str, "%llu", &offset) == 1)
1294 		swsusp_resume_block = offset;
1295 
1296 	return 1;
1297 }
1298 
hibernate_setup(char * str)1299 static int __init hibernate_setup(char *str)
1300 {
1301 	if (!strncmp(str, "noresume", 8)) {
1302 		noresume = 1;
1303 	} else if (!strncmp(str, "nocompress", 10)) {
1304 		nocompress = 1;
1305 	} else if (!strncmp(str, "no", 2)) {
1306 		noresume = 1;
1307 		nohibernate = 1;
1308 	} else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
1309 		   && !strncmp(str, "protect_image", 13)) {
1310 		enable_restore_image_protection();
1311 	}
1312 	return 1;
1313 }
1314 
noresume_setup(char * str)1315 static int __init noresume_setup(char *str)
1316 {
1317 	noresume = 1;
1318 	return 1;
1319 }
1320 
resumewait_setup(char * str)1321 static int __init resumewait_setup(char *str)
1322 {
1323 	resume_wait = 1;
1324 	return 1;
1325 }
1326 
resumedelay_setup(char * str)1327 static int __init resumedelay_setup(char *str)
1328 {
1329 	int rc = kstrtouint(str, 0, &resume_delay);
1330 
1331 	if (rc)
1332 		pr_warn("resumedelay: bad option string '%s'\n", str);
1333 	return 1;
1334 }
1335 
nohibernate_setup(char * str)1336 static int __init nohibernate_setup(char *str)
1337 {
1338 	noresume = 1;
1339 	nohibernate = 1;
1340 	return 1;
1341 }
1342 
1343 __setup("noresume", noresume_setup);
1344 __setup("resume_offset=", resume_offset_setup);
1345 __setup("resume=", resume_setup);
1346 __setup("hibernate=", hibernate_setup);
1347 __setup("resumewait", resumewait_setup);
1348 __setup("resumedelay=", resumedelay_setup);
1349 __setup("nohibernate", nohibernate_setup);
1350