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> |
1da177e4 LT |
52 | |
53 | #include "power.h" | |
54 | ||
ca0aec0f | 55 | /* |
853609b6 | 56 | * Preferred image size in bytes (tunable via /sys/power/image_size). |
ca0aec0f | 57 | * When it is set to N, swsusp will do its best to ensure the image |
853609b6 | 58 | * size will not exceed N bytes, but if that is impossible, it will |
ca0aec0f RW |
59 | * try to create the smallest image possible. |
60 | */ | |
853609b6 | 61 | unsigned long image_size = 500 * 1024 * 1024; |
ca0aec0f | 62 | |
f577eb30 RW |
63 | int in_suspend __nosavedata = 0; |
64 | ||
3448097f LT |
65 | #ifdef CONFIG_HIGHMEM |
66 | unsigned int count_highmem_pages(void); | |
3448097f LT |
67 | int restore_highmem(void); |
68 | #else | |
3448097f LT |
69 | static inline int restore_highmem(void) { return 0; } |
70 | static inline unsigned int count_highmem_pages(void) { return 0; } | |
71 | #endif | |
72 | ||
1da177e4 | 73 | /** |
f577eb30 RW |
74 | * The following functions are used for tracing the allocated |
75 | * swap pages, so that they can be freed in case of an error. | |
7088a5c0 | 76 | * |
f577eb30 | 77 | * The functions operate on a linked bitmap structure defined |
61159a31 | 78 | * in power.h |
1da177e4 | 79 | */ |
7088a5c0 | 80 | |
61159a31 | 81 | void free_bitmap(struct bitmap_page *bitmap) |
1da177e4 | 82 | { |
f577eb30 | 83 | struct bitmap_page *bp; |
1da177e4 | 84 | |
f577eb30 RW |
85 | while (bitmap) { |
86 | bp = bitmap->next; | |
87 | free_page((unsigned long)bitmap); | |
88 | bitmap = bp; | |
7088a5c0 RW |
89 | } |
90 | } | |
91 | ||
61159a31 | 92 | struct bitmap_page *alloc_bitmap(unsigned int nr_bits) |
7088a5c0 | 93 | { |
f577eb30 RW |
94 | struct bitmap_page *bitmap, *bp; |
95 | unsigned int n; | |
7088a5c0 | 96 | |
f577eb30 | 97 | if (!nr_bits) |
7088a5c0 RW |
98 | return NULL; |
99 | ||
f577eb30 RW |
100 | bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL); |
101 | bp = bitmap; | |
102 | for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) { | |
103 | bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL); | |
104 | bp = bp->next; | |
105 | if (!bp) { | |
106 | free_bitmap(bitmap); | |
7088a5c0 RW |
107 | return NULL; |
108 | } | |
1da177e4 | 109 | } |
f577eb30 | 110 | return bitmap; |
1da177e4 LT |
111 | } |
112 | ||
f577eb30 | 113 | static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit) |
1da177e4 | 114 | { |
f577eb30 RW |
115 | unsigned int n; |
116 | ||
117 | n = BITMAP_PAGE_BITS; | |
118 | while (bitmap && n <= bit) { | |
119 | n += BITMAP_PAGE_BITS; | |
120 | bitmap = bitmap->next; | |
121 | } | |
122 | if (!bitmap) | |
123 | return -EINVAL; | |
124 | n -= BITMAP_PAGE_BITS; | |
125 | bit -= n; | |
126 | n = 0; | |
127 | while (bit >= BITS_PER_CHUNK) { | |
128 | bit -= BITS_PER_CHUNK; | |
129 | n++; | |
7088a5c0 | 130 | } |
f577eb30 RW |
131 | bitmap->chunks[n] |= (1UL << bit); |
132 | return 0; | |
7088a5c0 | 133 | } |
1da177e4 | 134 | |
3aef83e0 | 135 | sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap) |
7088a5c0 | 136 | { |
f577eb30 RW |
137 | unsigned long offset; |
138 | ||
139 | offset = swp_offset(get_swap_page_of_type(swap)); | |
140 | if (offset) { | |
3aef83e0 | 141 | if (bitmap_set(bitmap, offset)) |
f577eb30 | 142 | swap_free(swp_entry(swap, offset)); |
3aef83e0 RW |
143 | else |
144 | return swapdev_block(swap, offset); | |
7088a5c0 | 145 | } |
3aef83e0 | 146 | return 0; |
7088a5c0 | 147 | } |
1da177e4 | 148 | |
61159a31 | 149 | void free_all_swap_pages(int swap, struct bitmap_page *bitmap) |
7088a5c0 | 150 | { |
f577eb30 RW |
151 | unsigned int bit, n; |
152 | unsigned long test; | |
7088a5c0 | 153 | |
f577eb30 RW |
154 | bit = 0; |
155 | while (bitmap) { | |
156 | for (n = 0; n < BITMAP_PAGE_CHUNKS; n++) | |
157 | for (test = 1UL; test; test <<= 1) { | |
158 | if (bitmap->chunks[n] & test) | |
159 | swap_free(swp_entry(swap, bit)); | |
160 | bit++; | |
161 | } | |
162 | bitmap = bitmap->next; | |
1da177e4 | 163 | } |
7088a5c0 RW |
164 | } |
165 | ||
72a97e08 RW |
166 | /** |
167 | * swsusp_shrink_memory - Try to free as much memory as needed | |
168 | * | |
169 | * ... but do not OOM-kill anyone | |
170 | * | |
171 | * Notice: all userland should be stopped before it is called, or | |
172 | * livelock is possible. | |
173 | */ | |
174 | ||
175 | #define SHRINK_BITE 10000 | |
d6277db4 RW |
176 | static inline unsigned long __shrink_memory(long tmp) |
177 | { | |
178 | if (tmp > SHRINK_BITE) | |
179 | tmp = SHRINK_BITE; | |
180 | return shrink_all_memory(tmp); | |
181 | } | |
72a97e08 RW |
182 | |
183 | int swsusp_shrink_memory(void) | |
184 | { | |
8357376d | 185 | long tmp; |
72a97e08 RW |
186 | struct zone *zone; |
187 | unsigned long pages = 0; | |
188 | unsigned int i = 0; | |
189 | char *p = "-\\|/"; | |
190 | ||
191 | printk("Shrinking memory... "); | |
192 | do { | |
8357376d RW |
193 | long size, highmem_size; |
194 | ||
195 | highmem_size = count_highmem_pages(); | |
196 | size = count_data_pages() + PAGES_FOR_IO; | |
b3a93a25 | 197 | tmp = size; |
8357376d | 198 | size += highmem_size; |
72a97e08 | 199 | for_each_zone (zone) |
8357376d RW |
200 | if (populated_zone(zone)) { |
201 | if (is_highmem(zone)) { | |
202 | highmem_size -= zone->free_pages; | |
203 | } else { | |
204 | tmp -= zone->free_pages; | |
205 | tmp += zone->lowmem_reserve[ZONE_NORMAL]; | |
206 | tmp += snapshot_additional_pages(zone); | |
207 | } | |
a938c356 | 208 | } |
8357376d RW |
209 | |
210 | if (highmem_size < 0) | |
211 | highmem_size = 0; | |
212 | ||
213 | tmp += highmem_size; | |
72a97e08 | 214 | if (tmp > 0) { |
d6277db4 | 215 | tmp = __shrink_memory(tmp); |
72a97e08 RW |
216 | if (!tmp) |
217 | return -ENOMEM; | |
218 | pages += tmp; | |
853609b6 | 219 | } else if (size > image_size / PAGE_SIZE) { |
d6277db4 | 220 | tmp = __shrink_memory(size - (image_size / PAGE_SIZE)); |
b3a93a25 | 221 | pages += tmp; |
72a97e08 | 222 | } |
72a97e08 RW |
223 | printk("\b%c", p[i++%4]); |
224 | } while (tmp > 0); | |
225 | printk("\bdone (%lu pages freed)\n", pages); | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
1da177e4 LT |
230 | int swsusp_suspend(void) |
231 | { | |
232 | int error; | |
0fbeb5a4 | 233 | |
1da177e4 LT |
234 | if ((error = arch_prepare_suspend())) |
235 | return error; | |
8357376d | 236 | |
1da177e4 LT |
237 | local_irq_disable(); |
238 | /* At this point, device_suspend() has been called, but *not* | |
239 | * device_power_down(). We *must* device_power_down() now. | |
240 | * Otherwise, drivers for some devices (e.g. interrupt controllers) | |
241 | * become desynchronized with the actual state of the hardware | |
242 | * at resume time, and evil weirdness ensues. | |
243 | */ | |
244 | if ((error = device_power_down(PMSG_FREEZE))) { | |
99dc7d63 | 245 | printk(KERN_ERR "Some devices failed to power down, aborting suspend\n"); |
0fbeb5a4 | 246 | goto Enable_irqs; |
1da177e4 | 247 | } |
47b724f3 | 248 | |
1da177e4 LT |
249 | save_processor_state(); |
250 | if ((error = swsusp_arch_suspend())) | |
99dc7d63 | 251 | printk(KERN_ERR "Error %d suspending\n", error); |
1da177e4 LT |
252 | /* Restore control flow magically appears here */ |
253 | restore_processor_state(); | |
f1cc0a89 DB |
254 | /* NOTE: device_power_up() is just a resume() for devices |
255 | * that suspended with irqs off ... no overall powerup. | |
256 | */ | |
1da177e4 | 257 | device_power_up(); |
0fbeb5a4 | 258 | Enable_irqs: |
1da177e4 LT |
259 | local_irq_enable(); |
260 | return error; | |
261 | } | |
262 | ||
263 | int swsusp_resume(void) | |
264 | { | |
265 | int error; | |
f1cc0a89 | 266 | |
1da177e4 | 267 | local_irq_disable(); |
f1cc0a89 DB |
268 | /* NOTE: device_power_down() is just a suspend() with irqs off; |
269 | * it has no special "power things down" semantics | |
270 | */ | |
271 | if (device_power_down(PMSG_PRETHAW)) | |
1da177e4 LT |
272 | printk(KERN_ERR "Some devices failed to power down, very bad\n"); |
273 | /* We'll ignore saved state, but this gets preempt count (etc) right */ | |
274 | save_processor_state(); | |
8357376d RW |
275 | error = restore_highmem(); |
276 | if (!error) { | |
277 | error = swsusp_arch_resume(); | |
278 | /* The code below is only ever reached in case of a failure. | |
279 | * Otherwise execution continues at place where | |
280 | * swsusp_arch_suspend() was called | |
281 | */ | |
282 | BUG_ON(!error); | |
283 | /* This call to restore_highmem() undos the previous one */ | |
284 | restore_highmem(); | |
285 | } | |
2c1b4a5c RW |
286 | /* The only reason why swsusp_arch_resume() can fail is memory being |
287 | * very tight, so we have to free it as soon as we can to avoid | |
288 | * subsequent failures | |
289 | */ | |
290 | swsusp_free(); | |
1da177e4 | 291 | restore_processor_state(); |
8446f1d3 | 292 | touch_softlockup_watchdog(); |
1da177e4 LT |
293 | device_power_up(); |
294 | local_irq_enable(); | |
295 | return error; | |
296 | } |