2 * linux/kernel/power/swsusp.c
4 * This file provides code to write suspend image to swap and read it back.
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
9 * This file is released under the GPLv2.
11 * I'd like to thank the following people for their work:
13 * Pavel Machek <pavel@ucw.cz>:
14 * Modifications, defectiveness pointing, being with me at the very beginning,
15 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
17 * Steve Doddi <dirk@loth.demon.co.uk>:
18 * Support the possibility of hardware state restoring.
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
28 * Andreas Mohr <a.mohr@mailto.de>
30 * Alex Badea <vampire@go.ro>:
33 * Rafael J. Wysocki <rjw@sisk.pl>
34 * Reworked the freeing of memory and the handling of swap
36 * More state savers are welcome. Especially for the scsi layer...
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
42 #include <linux/suspend.h>
43 #include <linux/spinlock.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/swap.h>
48 #include <linux/swapops.h>
49 #include <linux/bootmem.h>
50 #include <linux/syscalls.h>
51 #include <linux/highmem.h>
52 #include <linux/time.h>
57 * Preferred image size in bytes (tunable via /sys/power/image_size).
58 * When it is set to N, swsusp will do its best to ensure the image
59 * size will not exceed N bytes, but if that is impossible, it will
60 * try to create the smallest image possible.
62 unsigned long image_size
= 500 * 1024 * 1024;
64 int in_suspend __nosavedata
= 0;
67 unsigned int count_highmem_pages(void);
68 int restore_highmem(void);
70 static inline int restore_highmem(void) { return 0; }
71 static inline unsigned int count_highmem_pages(void) { return 0; }
75 * The following functions are used for tracing the allocated
76 * swap pages, so that they can be freed in case of an error.
78 * The functions operate on a linked bitmap structure defined
82 void free_bitmap(struct bitmap_page
*bitmap
)
84 struct bitmap_page
*bp
;
88 free_page((unsigned long)bitmap
);
93 struct bitmap_page
*alloc_bitmap(unsigned int nr_bits
)
95 struct bitmap_page
*bitmap
, *bp
;
101 bitmap
= (struct bitmap_page
*)get_zeroed_page(GFP_KERNEL
);
103 for (n
= BITMAP_PAGE_BITS
; n
< nr_bits
; n
+= BITMAP_PAGE_BITS
) {
104 bp
->next
= (struct bitmap_page
*)get_zeroed_page(GFP_KERNEL
);
114 static int bitmap_set(struct bitmap_page
*bitmap
, unsigned long bit
)
118 n
= BITMAP_PAGE_BITS
;
119 while (bitmap
&& n
<= bit
) {
120 n
+= BITMAP_PAGE_BITS
;
121 bitmap
= bitmap
->next
;
125 n
-= BITMAP_PAGE_BITS
;
128 while (bit
>= BITS_PER_CHUNK
) {
129 bit
-= BITS_PER_CHUNK
;
132 bitmap
->chunks
[n
] |= (1UL << bit
);
136 sector_t
alloc_swapdev_block(int swap
, struct bitmap_page
*bitmap
)
138 unsigned long offset
;
140 offset
= swp_offset(get_swap_page_of_type(swap
));
142 if (bitmap_set(bitmap
, offset
))
143 swap_free(swp_entry(swap
, offset
));
145 return swapdev_block(swap
, offset
);
150 void free_all_swap_pages(int swap
, struct bitmap_page
*bitmap
)
157 for (n
= 0; n
< BITMAP_PAGE_CHUNKS
; n
++)
158 for (test
= 1UL; test
; test
<<= 1) {
159 if (bitmap
->chunks
[n
] & test
)
160 swap_free(swp_entry(swap
, bit
));
163 bitmap
= bitmap
->next
;
168 * swsusp_show_speed - print the time elapsed between two events represented by
171 * @nr_pages - number of pages processed between @start and @stop
172 * @msg - introductory message to print
175 void swsusp_show_speed(struct timeval
*start
, struct timeval
*stop
,
176 unsigned nr_pages
, char *msg
)
178 s64 elapsed_centisecs64
;
183 elapsed_centisecs64
= timeval_to_ns(stop
) - timeval_to_ns(start
);
184 do_div(elapsed_centisecs64
, NSEC_PER_SEC
/ 100);
185 centisecs
= elapsed_centisecs64
;
187 centisecs
= 1; /* avoid div-by-zero */
188 k
= nr_pages
* (PAGE_SIZE
/ 1024);
189 kps
= (k
* 100) / centisecs
;
190 printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg
, k
,
191 centisecs
/ 100, centisecs
% 100,
192 kps
/ 1000, (kps
% 1000) / 10);
196 * swsusp_shrink_memory - Try to free as much memory as needed
198 * ... but do not OOM-kill anyone
200 * Notice: all userland should be stopped before it is called, or
201 * livelock is possible.
204 #define SHRINK_BITE 10000
205 static inline unsigned long __shrink_memory(long tmp
)
207 if (tmp
> SHRINK_BITE
)
209 return shrink_all_memory(tmp
);
212 int swsusp_shrink_memory(void)
216 unsigned long pages
= 0;
219 struct timeval start
, stop
;
221 printk("Shrinking memory... ");
222 do_gettimeofday(&start
);
224 long size
, highmem_size
;
226 highmem_size
= count_highmem_pages();
227 size
= count_data_pages() + PAGES_FOR_IO
;
229 size
+= highmem_size
;
231 if (populated_zone(zone
)) {
232 if (is_highmem(zone
)) {
233 highmem_size
-= zone
->free_pages
;
235 tmp
-= zone
->free_pages
;
236 tmp
+= zone
->lowmem_reserve
[ZONE_NORMAL
];
237 tmp
+= snapshot_additional_pages(zone
);
241 if (highmem_size
< 0)
246 tmp
= __shrink_memory(tmp
);
250 } else if (size
> image_size
/ PAGE_SIZE
) {
251 tmp
= __shrink_memory(size
- (image_size
/ PAGE_SIZE
));
254 printk("\b%c", p
[i
++%4]);
256 do_gettimeofday(&stop
);
257 printk("\bdone (%lu pages freed)\n", pages
);
258 swsusp_show_speed(&start
, &stop
, pages
, "Freed");
263 int swsusp_suspend(void)
267 if ((error
= arch_prepare_suspend()))
271 /* At this point, device_suspend() has been called, but *not*
272 * device_power_down(). We *must* device_power_down() now.
273 * Otherwise, drivers for some devices (e.g. interrupt controllers)
274 * become desynchronized with the actual state of the hardware
275 * at resume time, and evil weirdness ensues.
277 if ((error
= device_power_down(PMSG_FREEZE
))) {
278 printk(KERN_ERR
"Some devices failed to power down, aborting suspend\n");
282 save_processor_state();
283 if ((error
= swsusp_arch_suspend()))
284 printk(KERN_ERR
"Error %d suspending\n", error
);
285 /* Restore control flow magically appears here */
286 restore_processor_state();
287 /* NOTE: device_power_up() is just a resume() for devices
288 * that suspended with irqs off ... no overall powerup.
296 int swsusp_resume(void)
301 /* NOTE: device_power_down() is just a suspend() with irqs off;
302 * it has no special "power things down" semantics
304 if (device_power_down(PMSG_PRETHAW
))
305 printk(KERN_ERR
"Some devices failed to power down, very bad\n");
306 /* We'll ignore saved state, but this gets preempt count (etc) right */
307 save_processor_state();
308 error
= restore_highmem();
310 error
= swsusp_arch_resume();
311 /* The code below is only ever reached in case of a failure.
312 * Otherwise execution continues at place where
313 * swsusp_arch_suspend() was called
316 /* This call to restore_highmem() undos the previous one */
319 /* The only reason why swsusp_arch_resume() can fail is memory being
320 * very tight, so we have to free it as soon as we can to avoid
321 * subsequent failures
324 restore_processor_state();
325 touch_softlockup_watchdog();