Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/power/swsusp.c | |
3 | * | |
96bc7aec | 4 | * This file provides code to write suspend image to swap and read it back. |
1da177e4 LT |
5 | * |
6 | * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu> | |
25761b6e | 7 | * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz> |
1da177e4 LT |
8 | * |
9 | * This file is released under the GPLv2. | |
10 | * | |
11 | * I'd like to thank the following people for their work: | |
2e4d5822 | 12 | * |
1da177e4 LT |
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. | |
16 | * | |
2e4d5822 | 17 | * Steve Doddi <dirk@loth.demon.co.uk>: |
1da177e4 LT |
18 | * Support the possibility of hardware state restoring. |
19 | * | |
20 | * Raph <grey.havens@earthling.net>: | |
21 | * Support for preserving states of network devices and virtual console | |
22 | * (including X and svgatextmode) | |
23 | * | |
24 | * Kurt Garloff <garloff@suse.de>: | |
25 | * Straightened the critical function in order to prevent compilers from | |
26 | * playing tricks with local variables. | |
27 | * | |
28 | * Andreas Mohr <a.mohr@mailto.de> | |
29 | * | |
30 | * Alex Badea <vampire@go.ro>: | |
31 | * Fixed runaway init | |
32 | * | |
7088a5c0 | 33 | * Rafael J. Wysocki <rjw@sisk.pl> |
61159a31 | 34 | * Reworked the freeing of memory and the handling of swap |
7088a5c0 | 35 | * |
1da177e4 LT |
36 | * More state savers are welcome. Especially for the scsi layer... |
37 | * | |
38 | * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt | |
39 | */ | |
40 | ||
1da177e4 LT |
41 | #include <linux/mm.h> |
42 | #include <linux/suspend.h> | |
1da177e4 | 43 | #include <linux/spinlock.h> |
1da177e4 LT |
44 | #include <linux/kernel.h> |
45 | #include <linux/major.h> | |
46 | #include <linux/swap.h> | |
47 | #include <linux/pm.h> | |
1da177e4 LT |
48 | #include <linux/swapops.h> |
49 | #include <linux/bootmem.h> | |
50 | #include <linux/syscalls.h> | |
1da177e4 | 51 | #include <linux/highmem.h> |
0d3a9abe | 52 | #include <linux/time.h> |
1da177e4 LT |
53 | |
54 | #include "power.h" | |
55 | ||
ca0aec0f | 56 | /* |
853609b6 | 57 | * Preferred image size in bytes (tunable via /sys/power/image_size). |
ca0aec0f | 58 | * When it is set to N, swsusp will do its best to ensure the image |
853609b6 | 59 | * size will not exceed N bytes, but if that is impossible, it will |
ca0aec0f RW |
60 | * try to create the smallest image possible. |
61 | */ | |
853609b6 | 62 | unsigned long image_size = 500 * 1024 * 1024; |
ca0aec0f | 63 | |
f577eb30 RW |
64 | int in_suspend __nosavedata = 0; |
65 | ||
3448097f LT |
66 | #ifdef CONFIG_HIGHMEM |
67 | unsigned int count_highmem_pages(void); | |
3448097f LT |
68 | int restore_highmem(void); |
69 | #else | |
3448097f LT |
70 | static inline int restore_highmem(void) { return 0; } |
71 | static inline unsigned int count_highmem_pages(void) { return 0; } | |
72 | #endif | |
73 | ||
1da177e4 | 74 | /** |
f577eb30 RW |
75 | * The following functions are used for tracing the allocated |
76 | * swap pages, so that they can be freed in case of an error. | |
7088a5c0 | 77 | * |
f577eb30 | 78 | * The functions operate on a linked bitmap structure defined |
61159a31 | 79 | * in power.h |
1da177e4 | 80 | */ |
7088a5c0 | 81 | |
61159a31 | 82 | void free_bitmap(struct bitmap_page *bitmap) |
1da177e4 | 83 | { |
f577eb30 | 84 | struct bitmap_page *bp; |
1da177e4 | 85 | |
f577eb30 RW |
86 | while (bitmap) { |
87 | bp = bitmap->next; | |
88 | free_page((unsigned long)bitmap); | |
89 | bitmap = bp; | |
7088a5c0 RW |
90 | } |
91 | } | |
92 | ||
61159a31 | 93 | struct bitmap_page *alloc_bitmap(unsigned int nr_bits) |
7088a5c0 | 94 | { |
f577eb30 RW |
95 | struct bitmap_page *bitmap, *bp; |
96 | unsigned int n; | |
7088a5c0 | 97 | |
f577eb30 | 98 | if (!nr_bits) |
7088a5c0 RW |
99 | return NULL; |
100 | ||
f577eb30 RW |
101 | bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL); |
102 | bp = bitmap; | |
103 | for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) { | |
104 | bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL); | |
105 | bp = bp->next; | |
106 | if (!bp) { | |
107 | free_bitmap(bitmap); | |
7088a5c0 RW |
108 | return NULL; |
109 | } | |
1da177e4 | 110 | } |
f577eb30 | 111 | return bitmap; |
1da177e4 LT |
112 | } |
113 | ||
f577eb30 | 114 | static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit) |
1da177e4 | 115 | { |
f577eb30 RW |
116 | unsigned int n; |
117 | ||
118 | n = BITMAP_PAGE_BITS; | |
119 | while (bitmap && n <= bit) { | |
120 | n += BITMAP_PAGE_BITS; | |
121 | bitmap = bitmap->next; | |
122 | } | |
123 | if (!bitmap) | |
124 | return -EINVAL; | |
125 | n -= BITMAP_PAGE_BITS; | |
126 | bit -= n; | |
127 | n = 0; | |
128 | while (bit >= BITS_PER_CHUNK) { | |
129 | bit -= BITS_PER_CHUNK; | |
130 | n++; | |
7088a5c0 | 131 | } |
f577eb30 RW |
132 | bitmap->chunks[n] |= (1UL << bit); |
133 | return 0; | |
7088a5c0 | 134 | } |
1da177e4 | 135 | |
3aef83e0 | 136 | sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap) |
7088a5c0 | 137 | { |
f577eb30 RW |
138 | unsigned long offset; |
139 | ||
140 | offset = swp_offset(get_swap_page_of_type(swap)); | |
141 | if (offset) { | |
3aef83e0 | 142 | if (bitmap_set(bitmap, offset)) |
f577eb30 | 143 | swap_free(swp_entry(swap, offset)); |
3aef83e0 RW |
144 | else |
145 | return swapdev_block(swap, offset); | |
7088a5c0 | 146 | } |
3aef83e0 | 147 | return 0; |
7088a5c0 | 148 | } |
1da177e4 | 149 | |
61159a31 | 150 | void free_all_swap_pages(int swap, struct bitmap_page *bitmap) |
7088a5c0 | 151 | { |
f577eb30 RW |
152 | unsigned int bit, n; |
153 | unsigned long test; | |
7088a5c0 | 154 | |
f577eb30 RW |
155 | bit = 0; |
156 | while (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)); | |
161 | bit++; | |
162 | } | |
163 | bitmap = bitmap->next; | |
1da177e4 | 164 | } |
7088a5c0 RW |
165 | } |
166 | ||
0d3a9abe RW |
167 | /** |
168 | * swsusp_show_speed - print the time elapsed between two events represented by | |
169 | * @start and @stop | |
170 | * | |
171 | * @nr_pages - number of pages processed between @start and @stop | |
172 | * @msg - introductory message to print | |
173 | */ | |
174 | ||
175 | void swsusp_show_speed(struct timeval *start, struct timeval *stop, | |
176 | unsigned nr_pages, char *msg) | |
177 | { | |
178 | s64 elapsed_centisecs64; | |
179 | int centisecs; | |
180 | int k; | |
181 | int kps; | |
182 | ||
183 | elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start); | |
184 | do_div(elapsed_centisecs64, NSEC_PER_SEC / 100); | |
185 | centisecs = elapsed_centisecs64; | |
186 | if (centisecs == 0) | |
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); | |
193 | } | |
194 | ||
72a97e08 RW |
195 | /** |
196 | * swsusp_shrink_memory - Try to free as much memory as needed | |
197 | * | |
198 | * ... but do not OOM-kill anyone | |
199 | * | |
200 | * Notice: all userland should be stopped before it is called, or | |
201 | * livelock is possible. | |
202 | */ | |
203 | ||
204 | #define SHRINK_BITE 10000 | |
d6277db4 RW |
205 | static inline unsigned long __shrink_memory(long tmp) |
206 | { | |
207 | if (tmp > SHRINK_BITE) | |
208 | tmp = SHRINK_BITE; | |
209 | return shrink_all_memory(tmp); | |
210 | } | |
72a97e08 RW |
211 | |
212 | int swsusp_shrink_memory(void) | |
213 | { | |
8357376d | 214 | long tmp; |
72a97e08 RW |
215 | struct zone *zone; |
216 | unsigned long pages = 0; | |
217 | unsigned int i = 0; | |
218 | char *p = "-\\|/"; | |
0d3a9abe | 219 | struct timeval start, stop; |
72a97e08 RW |
220 | |
221 | printk("Shrinking memory... "); | |
0d3a9abe | 222 | do_gettimeofday(&start); |
72a97e08 | 223 | do { |
8357376d RW |
224 | long size, highmem_size; |
225 | ||
226 | highmem_size = count_highmem_pages(); | |
227 | size = count_data_pages() + PAGES_FOR_IO; | |
b3a93a25 | 228 | tmp = size; |
8357376d | 229 | size += highmem_size; |
72a97e08 | 230 | for_each_zone (zone) |
8357376d RW |
231 | if (populated_zone(zone)) { |
232 | if (is_highmem(zone)) { | |
d23ad423 CL |
233 | highmem_size -= |
234 | zone_page_state(zone, NR_FREE_PAGES); | |
8357376d | 235 | } else { |
d23ad423 | 236 | tmp -= zone_page_state(zone, NR_FREE_PAGES); |
8357376d RW |
237 | tmp += zone->lowmem_reserve[ZONE_NORMAL]; |
238 | tmp += snapshot_additional_pages(zone); | |
239 | } | |
a938c356 | 240 | } |
8357376d RW |
241 | |
242 | if (highmem_size < 0) | |
243 | highmem_size = 0; | |
244 | ||
245 | tmp += highmem_size; | |
72a97e08 | 246 | if (tmp > 0) { |
d6277db4 | 247 | tmp = __shrink_memory(tmp); |
72a97e08 RW |
248 | if (!tmp) |
249 | return -ENOMEM; | |
250 | pages += tmp; | |
853609b6 | 251 | } else if (size > image_size / PAGE_SIZE) { |
d6277db4 | 252 | tmp = __shrink_memory(size - (image_size / PAGE_SIZE)); |
b3a93a25 | 253 | pages += tmp; |
72a97e08 | 254 | } |
72a97e08 RW |
255 | printk("\b%c", p[i++%4]); |
256 | } while (tmp > 0); | |
0d3a9abe | 257 | do_gettimeofday(&stop); |
72a97e08 | 258 | printk("\bdone (%lu pages freed)\n", pages); |
0d3a9abe | 259 | swsusp_show_speed(&start, &stop, pages, "Freed"); |
72a97e08 RW |
260 | |
261 | return 0; | |
262 | } | |
263 | ||
1da177e4 LT |
264 | int swsusp_suspend(void) |
265 | { | |
266 | int error; | |
0fbeb5a4 | 267 | |
1da177e4 LT |
268 | if ((error = arch_prepare_suspend())) |
269 | return error; | |
8357376d | 270 | |
1da177e4 LT |
271 | local_irq_disable(); |
272 | /* At this point, device_suspend() has been called, but *not* | |
273 | * device_power_down(). We *must* device_power_down() now. | |
274 | * Otherwise, drivers for some devices (e.g. interrupt controllers) | |
275 | * become desynchronized with the actual state of the hardware | |
276 | * at resume time, and evil weirdness ensues. | |
277 | */ | |
278 | if ((error = device_power_down(PMSG_FREEZE))) { | |
99dc7d63 | 279 | printk(KERN_ERR "Some devices failed to power down, aborting suspend\n"); |
0fbeb5a4 | 280 | goto Enable_irqs; |
1da177e4 | 281 | } |
47b724f3 | 282 | |
1da177e4 LT |
283 | save_processor_state(); |
284 | if ((error = swsusp_arch_suspend())) | |
99dc7d63 | 285 | printk(KERN_ERR "Error %d suspending\n", error); |
1da177e4 LT |
286 | /* Restore control flow magically appears here */ |
287 | restore_processor_state(); | |
f1cc0a89 DB |
288 | /* NOTE: device_power_up() is just a resume() for devices |
289 | * that suspended with irqs off ... no overall powerup. | |
290 | */ | |
1da177e4 | 291 | device_power_up(); |
59a49335 | 292 | Enable_irqs: |
1da177e4 LT |
293 | local_irq_enable(); |
294 | return error; | |
295 | } | |
296 | ||
297 | int swsusp_resume(void) | |
298 | { | |
299 | int error; | |
f1cc0a89 | 300 | |
1da177e4 | 301 | local_irq_disable(); |
f1cc0a89 DB |
302 | /* NOTE: device_power_down() is just a suspend() with irqs off; |
303 | * it has no special "power things down" semantics | |
304 | */ | |
305 | if (device_power_down(PMSG_PRETHAW)) | |
1da177e4 LT |
306 | printk(KERN_ERR "Some devices failed to power down, very bad\n"); |
307 | /* We'll ignore saved state, but this gets preempt count (etc) right */ | |
308 | save_processor_state(); | |
8357376d RW |
309 | error = restore_highmem(); |
310 | if (!error) { | |
311 | error = swsusp_arch_resume(); | |
312 | /* The code below is only ever reached in case of a failure. | |
313 | * Otherwise execution continues at place where | |
314 | * swsusp_arch_suspend() was called | |
315 | */ | |
316 | BUG_ON(!error); | |
317 | /* This call to restore_highmem() undos the previous one */ | |
318 | restore_highmem(); | |
319 | } | |
2c1b4a5c RW |
320 | /* The only reason why swsusp_arch_resume() can fail is memory being |
321 | * very tight, so we have to free it as soon as we can to avoid | |
322 | * subsequent failures | |
323 | */ | |
324 | swsusp_free(); | |
1da177e4 | 325 | restore_processor_state(); |
8446f1d3 | 326 | touch_softlockup_watchdog(); |
1da177e4 LT |
327 | device_power_up(); |
328 | local_irq_enable(); | |
329 | return error; | |
330 | } |