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