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