swsusp.c 7.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * linux/kernel/power/swsusp.c
 *
4
 * This file provides code to write suspend image to swap and read it back.
L
Linus Torvalds 已提交
5 6
 *
 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7
 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
L
Linus Torvalds 已提交
8 9 10 11
 *
 * This file is released under the GPLv2.
 *
 * I'd like to thank the following people for their work:
12
 *
L
Linus Torvalds 已提交
13 14 15 16
 * Pavel Machek <pavel@ucw.cz>:
 * Modifications, defectiveness pointing, being with me at the very beginning,
 * 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>:
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Support the possibility of hardware state restoring.
 *
 * Raph <grey.havens@earthling.net>:
 * Support for preserving states of network devices and virtual console
 * (including X and svgatextmode)
 *
 * Kurt Garloff <garloff@suse.de>:
 * Straightened the critical function in order to prevent compilers from
 * playing tricks with local variables.
 *
 * Andreas Mohr <a.mohr@mailto.de>
 *
 * Alex Badea <vampire@go.ro>:
 * Fixed runaway init
 *
33
 * Rafael J. Wysocki <rjw@sisk.pl>
34
 * Reworked the freeing of memory and the handling of swap
35
 *
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 * More state savers are welcome. Especially for the scsi layer...
 *
 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
 */

#include <linux/mm.h>
#include <linux/suspend.h>
#include <linux/spinlock.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/swap.h>
#include <linux/pm.h>
#include <linux/swapops.h>
#include <linux/bootmem.h>
#include <linux/syscalls.h>
#include <linux/highmem.h>

#include "power.h"

55
/*
56
 * Preferred image size in bytes (tunable via /sys/power/image_size).
57
 * When it is set to N, swsusp will do its best to ensure the image
58
 * size will not exceed N bytes, but if that is impossible, it will
59 60
 * try to create the smallest image possible.
 */
61
unsigned long image_size = 500 * 1024 * 1024;
62

63 64
int in_suspend __nosavedata = 0;

65 66 67 68 69 70 71 72
#ifdef CONFIG_HIGHMEM
unsigned int count_highmem_pages(void);
int restore_highmem(void);
#else
static inline int restore_highmem(void) { return 0; }
static inline unsigned int count_highmem_pages(void) { return 0; }
#endif

L
Linus Torvalds 已提交
73
/**
74 75
 *	The following functions are used for tracing the allocated
 *	swap pages, so that they can be freed in case of an error.
76
 *
77
 *	The functions operate on a linked bitmap structure defined
78
 *	in power.h
L
Linus Torvalds 已提交
79
 */
80

81
void free_bitmap(struct bitmap_page *bitmap)
L
Linus Torvalds 已提交
82
{
83
	struct bitmap_page *bp;
L
Linus Torvalds 已提交
84

85 86 87 88
	while (bitmap) {
		bp = bitmap->next;
		free_page((unsigned long)bitmap);
		bitmap = bp;
89 90 91
	}
}

92
struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
93
{
94 95
	struct bitmap_page *bitmap, *bp;
	unsigned int n;
96

97
	if (!nr_bits)
98 99
		return NULL;

100 101 102 103 104 105 106
	bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
	bp = bitmap;
	for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
		bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
		bp = bp->next;
		if (!bp) {
			free_bitmap(bitmap);
107 108
			return NULL;
		}
L
Linus Torvalds 已提交
109
	}
110
	return bitmap;
L
Linus Torvalds 已提交
111 112
}

113
static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
L
Linus Torvalds 已提交
114
{
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	unsigned int n;

	n = BITMAP_PAGE_BITS;
	while (bitmap && n <= bit) {
		n += BITMAP_PAGE_BITS;
		bitmap = bitmap->next;
	}
	if (!bitmap)
		return -EINVAL;
	n -= BITMAP_PAGE_BITS;
	bit -= n;
	n = 0;
	while (bit >= BITS_PER_CHUNK) {
		bit -= BITS_PER_CHUNK;
		n++;
130
	}
131 132
	bitmap->chunks[n] |= (1UL << bit);
	return 0;
133
}
L
Linus Torvalds 已提交
134

135
sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap)
136
{
137 138 139 140
	unsigned long offset;

	offset = swp_offset(get_swap_page_of_type(swap));
	if (offset) {
141
		if (bitmap_set(bitmap, offset))
142
			swap_free(swp_entry(swap, offset));
143 144
		else
			return swapdev_block(swap, offset);
145
	}
146
	return 0;
147
}
L
Linus Torvalds 已提交
148

149
void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
150
{
151 152
	unsigned int bit, n;
	unsigned long test;
153

154 155 156 157 158 159 160 161 162
	bit = 0;
	while (bitmap) {
		for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
			for (test = 1UL; test; test <<= 1) {
				if (bitmap->chunks[n] & test)
					swap_free(swp_entry(swap, bit));
				bit++;
			}
		bitmap = bitmap->next;
L
Linus Torvalds 已提交
163
	}
164 165
}

166 167 168 169 170 171 172 173 174 175
/**
 *	swsusp_shrink_memory -  Try to free as much memory as needed
 *
 *	... but do not OOM-kill anyone
 *
 *	Notice: all userland should be stopped before it is called, or
 *	livelock is possible.
 */

#define SHRINK_BITE	10000
176 177 178 179 180 181
static inline unsigned long __shrink_memory(long tmp)
{
	if (tmp > SHRINK_BITE)
		tmp = SHRINK_BITE;
	return shrink_all_memory(tmp);
}
182 183 184

int swsusp_shrink_memory(void)
{
185
	long tmp;
186 187 188 189 190 191 192
	struct zone *zone;
	unsigned long pages = 0;
	unsigned int i = 0;
	char *p = "-\\|/";

	printk("Shrinking memory...  ");
	do {
193 194 195 196
		long size, highmem_size;

		highmem_size = count_highmem_pages();
		size = count_data_pages() + PAGES_FOR_IO;
197
		tmp = size;
198
		size += highmem_size;
199
		for_each_zone (zone)
200 201 202 203 204 205 206 207
			if (populated_zone(zone)) {
				if (is_highmem(zone)) {
					highmem_size -= zone->free_pages;
				} else {
					tmp -= zone->free_pages;
					tmp += zone->lowmem_reserve[ZONE_NORMAL];
					tmp += snapshot_additional_pages(zone);
				}
208
			}
209 210 211 212 213

		if (highmem_size < 0)
			highmem_size = 0;

		tmp += highmem_size;
214
		if (tmp > 0) {
215
			tmp = __shrink_memory(tmp);
216 217 218
			if (!tmp)
				return -ENOMEM;
			pages += tmp;
219
		} else if (size > image_size / PAGE_SIZE) {
220
			tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
221
			pages += tmp;
222 223 224 225 226 227 228 229
		}
		printk("\b%c", p[i++%4]);
	} while (tmp > 0);
	printk("\bdone (%lu pages freed)\n", pages);

	return 0;
}

L
Linus Torvalds 已提交
230 231 232
int swsusp_suspend(void)
{
	int error;
233

L
Linus Torvalds 已提交
234 235
	if ((error = arch_prepare_suspend()))
		return error;
236

L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244
	local_irq_disable();
	/* At this point, device_suspend() has been called, but *not*
	 * device_power_down(). We *must* device_power_down() now.
	 * Otherwise, drivers for some devices (e.g. interrupt controllers)
	 * become desynchronized with the actual state of the hardware
	 * at resume time, and evil weirdness ensues.
	 */
	if ((error = device_power_down(PMSG_FREEZE))) {
245
		printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
246
		goto Enable_irqs;
L
Linus Torvalds 已提交
247
	}
248

L
Linus Torvalds 已提交
249 250
	save_processor_state();
	if ((error = swsusp_arch_suspend()))
251
		printk(KERN_ERR "Error %d suspending\n", error);
L
Linus Torvalds 已提交
252 253
	/* Restore control flow magically appears here */
	restore_processor_state();
D
David Brownell 已提交
254 255 256
	/* NOTE:  device_power_up() is just a resume() for devices
	 * that suspended with irqs off ... no overall powerup.
	 */
L
Linus Torvalds 已提交
257
	device_power_up();
258
Enable_irqs:
L
Linus Torvalds 已提交
259 260 261 262 263 264 265
	local_irq_enable();
	return error;
}

int swsusp_resume(void)
{
	int error;
D
David Brownell 已提交
266

L
Linus Torvalds 已提交
267
	local_irq_disable();
D
David Brownell 已提交
268 269 270 271
	/* NOTE:  device_power_down() is just a suspend() with irqs off;
	 * it has no special "power things down" semantics
	 */
	if (device_power_down(PMSG_PRETHAW))
L
Linus Torvalds 已提交
272 273 274
		printk(KERN_ERR "Some devices failed to power down, very bad\n");
	/* We'll ignore saved state, but this gets preempt count (etc) right */
	save_processor_state();
275 276 277 278 279 280 281 282 283 284 285
	error = restore_highmem();
	if (!error) {
		error = swsusp_arch_resume();
		/* The code below is only ever reached in case of a failure.
		 * Otherwise execution continues at place where
		 * swsusp_arch_suspend() was called
        	 */
		BUG_ON(!error);
		/* This call to restore_highmem() undos the previous one */
		restore_highmem();
	}
286 287 288 289 290
	/* The only reason why swsusp_arch_resume() can fail is memory being
	 * very tight, so we have to free it as soon as we can to avoid
	 * subsequent failures
	 */
	swsusp_free();
L
Linus Torvalds 已提交
291
	restore_processor_state();
I
Ingo Molnar 已提交
292
	touch_softlockup_watchdog();
L
Linus Torvalds 已提交
293 294 295 296
	device_power_up();
	local_irq_enable();
	return error;
}