blob: 5da304c8f1f66473233d68d713a66e2a17c59d54 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/power/swsusp.c
3 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provides code to write suspend image to swap and read it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 *
Pavel Machek2e4d5822005-06-25 14:55:12 -070017 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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 *
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080033 * Rafael J. Wysocki <rjw@sisk.pl>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080034 * Reworked the freeing of memory and the handling of swap
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080035 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * More state savers are welcome. Especially for the scsi layer...
37 *
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mm.h>
42#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/kernel.h>
45#include <linux/major.h>
46#include <linux/swap.h>
47#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/swapops.h>
49#include <linux/bootmem.h>
50#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/highmem.h>
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -080052#include <linux/time.h>
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -070053#include <linux/rbtree.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include "power.h"
56
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080057/*
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080058 * Preferred image size in bytes (tunable via /sys/power/image_size).
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080059 * When it is set to N, swsusp will do its best to ensure the image
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080060 * size will not exceed N bytes, but if that is impossible, it will
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080061 * try to create the smallest image possible.
62 */
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080063unsigned long image_size = 500 * 1024 * 1024;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080064
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080065int in_suspend __nosavedata = 0;
66
Linus Torvalds34480972006-06-25 18:41:00 -070067#ifdef CONFIG_HIGHMEM
68unsigned int count_highmem_pages(void);
Linus Torvalds34480972006-06-25 18:41:00 -070069int restore_highmem(void);
70#else
Linus Torvalds34480972006-06-25 18:41:00 -070071static inline int restore_highmem(void) { return 0; }
72static inline unsigned int count_highmem_pages(void) { return 0; }
73#endif
74
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080075/**
76 * The following functions are used for tracing the allocated
77 * swap pages, so that they can be freed in case of an error.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080078 */
79
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -070080struct swsusp_extent {
81 struct rb_node node;
82 unsigned long start;
83 unsigned long end;
84};
85
86static struct rb_root swsusp_extents = RB_ROOT;
87
88static int swsusp_extents_insert(unsigned long swap_offset)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080089{
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -070090 struct rb_node **new = &(swsusp_extents.rb_node);
91 struct rb_node *parent = NULL;
92 struct swsusp_extent *ext;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080093
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -070094 /* Figure out where to put the new node */
95 while (*new) {
96 ext = container_of(*new, struct swsusp_extent, node);
97 parent = *new;
98 if (swap_offset < ext->start) {
99 /* Try to merge */
100 if (swap_offset == ext->start - 1) {
101 ext->start--;
102 return 0;
103 }
104 new = &((*new)->rb_left);
105 } else if (swap_offset > ext->end) {
106 /* Try to merge */
107 if (swap_offset == ext->end + 1) {
108 ext->end++;
109 return 0;
110 }
111 new = &((*new)->rb_right);
112 } else {
113 /* It already is in the tree */
114 return -EINVAL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800115 }
116 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700117 /* Add the new node and rebalance the tree. */
118 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
119 if (!ext)
120 return -ENOMEM;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800121
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700122 ext->start = swap_offset;
123 ext->end = swap_offset;
124 rb_link_node(&ext->node, parent, new);
125 rb_insert_color(&ext->node, &swsusp_extents);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800126 return 0;
127}
128
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700129/**
130 * alloc_swapdev_block - allocate a swap page and register that it has
131 * been allocated, so that it can be freed in case of an error.
132 */
133
134sector_t alloc_swapdev_block(int swap)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800135{
136 unsigned long offset;
137
138 offset = swp_offset(get_swap_page_of_type(swap));
139 if (offset) {
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700140 if (swsusp_extents_insert(offset))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800141 swap_free(swp_entry(swap, offset));
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800142 else
143 return swapdev_block(swap, offset);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800144 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800145 return 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800146}
147
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700148/**
149 * free_all_swap_pages - free swap pages allocated for saving image data.
150 * It also frees the extents used to register which swap entres had been
151 * allocated.
152 */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800153
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700154void free_all_swap_pages(int swap)
155{
156 struct rb_node *node;
157
158 while ((node = swsusp_extents.rb_node)) {
159 struct swsusp_extent *ext;
160 unsigned long offset;
161
162 ext = container_of(node, struct swsusp_extent, node);
163 rb_erase(node, &swsusp_extents);
164 for (offset = ext->start; offset <= ext->end; offset++)
165 swap_free(swp_entry(swap, offset));
166
167 kfree(ext);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800168 }
169}
170
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700171int swsusp_swap_in_use(void)
172{
173 return (swsusp_extents.rb_node != NULL);
174}
175
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800176/**
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800177 * swsusp_show_speed - print the time elapsed between two events represented by
178 * @start and @stop
179 *
180 * @nr_pages - number of pages processed between @start and @stop
181 * @msg - introductory message to print
182 */
183
184void swsusp_show_speed(struct timeval *start, struct timeval *stop,
185 unsigned nr_pages, char *msg)
186{
187 s64 elapsed_centisecs64;
188 int centisecs;
189 int k;
190 int kps;
191
192 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
193 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
194 centisecs = elapsed_centisecs64;
195 if (centisecs == 0)
196 centisecs = 1; /* avoid div-by-zero */
197 k = nr_pages * (PAGE_SIZE / 1024);
198 kps = (k * 100) / centisecs;
199 printk("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
200 centisecs / 100, centisecs % 100,
201 kps / 1000, (kps % 1000) / 10);
202}
203
204/**
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800205 * swsusp_shrink_memory - Try to free as much memory as needed
206 *
207 * ... but do not OOM-kill anyone
208 *
209 * Notice: all userland should be stopped before it is called, or
210 * livelock is possible.
211 */
212
213#define SHRINK_BITE 10000
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700214static inline unsigned long __shrink_memory(long tmp)
215{
216 if (tmp > SHRINK_BITE)
217 tmp = SHRINK_BITE;
218 return shrink_all_memory(tmp);
219}
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800220
221int swsusp_shrink_memory(void)
222{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800223 long tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800224 struct zone *zone;
225 unsigned long pages = 0;
226 unsigned int i = 0;
227 char *p = "-\\|/";
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800228 struct timeval start, stop;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800229
230 printk("Shrinking memory... ");
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800231 do_gettimeofday(&start);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800232 do {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800233 long size, highmem_size;
234
235 highmem_size = count_highmem_pages();
Rafael J. Wysocki56f99bc2007-05-06 14:50:52 -0700236 size = count_data_pages() + PAGES_FOR_IO + SPARE_PAGES;
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800237 tmp = size;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800238 size += highmem_size;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800239 for_each_zone (zone)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800240 if (populated_zone(zone)) {
Rafael J. Wysockic75fd0e2007-04-04 19:08:21 -0700241 tmp += snapshot_additional_pages(zone);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800242 if (is_highmem(zone)) {
Christoph Lameterd23ad422007-02-10 01:43:02 -0800243 highmem_size -=
244 zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800245 } else {
Christoph Lameterd23ad422007-02-10 01:43:02 -0800246 tmp -= zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800247 tmp += zone->lowmem_reserve[ZONE_NORMAL];
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800248 }
Rafael J. Wysockia938c352006-06-23 02:04:46 -0700249 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800250
251 if (highmem_size < 0)
252 highmem_size = 0;
253
254 tmp += highmem_size;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800255 if (tmp > 0) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700256 tmp = __shrink_memory(tmp);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800257 if (!tmp)
258 return -ENOMEM;
259 pages += tmp;
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800260 } else if (size > image_size / PAGE_SIZE) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700261 tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800262 pages += tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800263 }
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800264 printk("\b%c", p[i++%4]);
265 } while (tmp > 0);
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800266 do_gettimeofday(&stop);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800267 printk("\bdone (%lu pages freed)\n", pages);
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800268 swsusp_show_speed(&start, &stop, pages, "Freed");
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800269
270 return 0;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273int swsusp_suspend(void)
274{
275 int error;
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if ((error = arch_prepare_suspend()))
278 return error;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 local_irq_disable();
281 /* At this point, device_suspend() has been called, but *not*
282 * device_power_down(). We *must* device_power_down() now.
283 * Otherwise, drivers for some devices (e.g. interrupt controllers)
284 * become desynchronized with the actual state of the hardware
285 * at resume time, and evil weirdness ensues.
286 */
287 if ((error = device_power_down(PMSG_FREEZE))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700288 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800289 goto Enable_irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 save_processor_state();
293 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700294 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Restore control flow magically appears here */
296 restore_processor_state();
David Brownellf1cc0a82006-08-14 23:11:08 -0700297 /* NOTE: device_power_up() is just a resume() for devices
298 * that suspended with irqs off ... no overall powerup.
299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 device_power_up();
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800301 Enable_irqs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 local_irq_enable();
303 return error;
304}
305
306int swsusp_resume(void)
307{
308 int error;
David Brownellf1cc0a82006-08-14 23:11:08 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 local_irq_disable();
David Brownellf1cc0a82006-08-14 23:11:08 -0700311 /* NOTE: device_power_down() is just a suspend() with irqs off;
312 * it has no special "power things down" semantics
313 */
314 if (device_power_down(PMSG_PRETHAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 printk(KERN_ERR "Some devices failed to power down, very bad\n");
316 /* We'll ignore saved state, but this gets preempt count (etc) right */
317 save_processor_state();
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800318 error = restore_highmem();
319 if (!error) {
320 error = swsusp_arch_resume();
321 /* The code below is only ever reached in case of a failure.
322 * Otherwise execution continues at place where
323 * swsusp_arch_suspend() was called
324 */
325 BUG_ON(!error);
326 /* This call to restore_highmem() undos the previous one */
327 restore_highmem();
328 }
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800329 /* The only reason why swsusp_arch_resume() can fail is memory being
330 * very tight, so we have to free it as soon as we can to avoid
331 * subsequent failures
332 */
333 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 restore_processor_state();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700335 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 device_power_up();
337 local_irq_enable();
338 return error;
339}