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