swsusp.c 9.2 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
 * 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>
52
#include <linux/time.h>
53
#include <linux/rbtree.h>
54
#include <linux/io.h>
L
Linus Torvalds 已提交
55 56 57

#include "power.h"

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

66 67
int in_suspend __nosavedata = 0;

L
Linus Torvalds 已提交
68
/**
69 70
 *	The following functions are used for tracing the allocated
 *	swap pages, so that they can be freed in case of an error.
L
Linus Torvalds 已提交
71
 */
72

73 74 75 76 77
struct swsusp_extent {
	struct rb_node node;
	unsigned long start;
	unsigned long end;
};
L
Linus Torvalds 已提交
78

79
static struct rb_root swsusp_extents = RB_ROOT;
80

81
static int swsusp_extents_insert(unsigned long swap_offset)
82
{
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	struct rb_node **new = &(swsusp_extents.rb_node);
	struct rb_node *parent = NULL;
	struct swsusp_extent *ext;

	/* Figure out where to put the new node */
	while (*new) {
		ext = container_of(*new, struct swsusp_extent, node);
		parent = *new;
		if (swap_offset < ext->start) {
			/* Try to merge */
			if (swap_offset == ext->start - 1) {
				ext->start--;
				return 0;
			}
			new = &((*new)->rb_left);
		} else if (swap_offset > ext->end) {
			/* Try to merge */
			if (swap_offset == ext->end + 1) {
				ext->end++;
				return 0;
			}
			new = &((*new)->rb_right);
		} else {
			/* It already is in the tree */
			return -EINVAL;
108
		}
L
Linus Torvalds 已提交
109
	}
110 111 112 113 114 115 116 117 118
	/* Add the new node and rebalance the tree. */
	ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
	if (!ext)
		return -ENOMEM;

	ext->start = swap_offset;
	ext->end = swap_offset;
	rb_link_node(&ext->node, parent, new);
	rb_insert_color(&ext->node, &swsusp_extents);
119
	return 0;
120
}
L
Linus Torvalds 已提交
121

122 123 124 125 126 127
/**
 *	alloc_swapdev_block - allocate a swap page and register that it has
 *	been allocated, so that it can be freed in case of an error.
 */

sector_t alloc_swapdev_block(int swap)
128
{
129 130 131 132
	unsigned long offset;

	offset = swp_offset(get_swap_page_of_type(swap));
	if (offset) {
133
		if (swsusp_extents_insert(offset))
134
			swap_free(swp_entry(swap, offset));
135 136
		else
			return swapdev_block(swap, offset);
137
	}
138
	return 0;
139
}
L
Linus Torvalds 已提交
140

141 142 143 144 145 146 147
/**
 *	free_all_swap_pages - free swap pages allocated for saving image data.
 *	It also frees the extents used to register which swap entres had been
 *	allocated.
 */

void free_all_swap_pages(int swap)
148
{
149 150 151 152 153 154 155 156 157 158 159 160
	struct rb_node *node;

	while ((node = swsusp_extents.rb_node)) {
		struct swsusp_extent *ext;
		unsigned long offset;

		ext = container_of(node, struct swsusp_extent, node);
		rb_erase(node, &swsusp_extents);
		for (offset = ext->start; offset <= ext->end; offset++)
			swap_free(swp_entry(swap, offset));

		kfree(ext);
L
Linus Torvalds 已提交
161
	}
162 163
}

164 165 166 167 168
int swsusp_swap_in_use(void)
{
	return (swsusp_extents.rb_node != NULL);
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 *	swsusp_show_speed - print the time elapsed between two events represented by
 *	@start and @stop
 *
 *	@nr_pages -	number of pages processed between @start and @stop
 *	@msg -		introductory message to print
 */

void swsusp_show_speed(struct timeval *start, struct timeval *stop,
			unsigned nr_pages, char *msg)
{
	s64 elapsed_centisecs64;
	int centisecs;
	int k;
	int kps;

	elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
	do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
	centisecs = elapsed_centisecs64;
	if (centisecs == 0)
		centisecs = 1;	/* avoid div-by-zero */
	k = nr_pages * (PAGE_SIZE / 1024);
	kps = (k * 100) / centisecs;
R
Rafael J. Wysocki 已提交
192 193
	printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
			msg, k,
194 195 196 197
			centisecs / 100, centisecs % 100,
			kps / 1000, (kps % 1000) / 10);
}

198 199 200 201 202 203 204 205 206 207
/**
 *	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
208 209 210 211 212 213
static inline unsigned long __shrink_memory(long tmp)
{
	if (tmp > SHRINK_BITE)
		tmp = SHRINK_BITE;
	return shrink_all_memory(tmp);
}
214 215 216

int swsusp_shrink_memory(void)
{
217
	long tmp;
218 219 220 221
	struct zone *zone;
	unsigned long pages = 0;
	unsigned int i = 0;
	char *p = "-\\|/";
222
	struct timeval start, stop;
223

R
Rafael J. Wysocki 已提交
224
	printk(KERN_INFO "PM: Shrinking memory...  ");
225
	do_gettimeofday(&start);
226
	do {
227 228 229
		long size, highmem_size;

		highmem_size = count_highmem_pages();
R
Rafael J. Wysocki 已提交
230
		size = count_data_pages() + PAGES_FOR_IO + SPARE_PAGES;
231
		tmp = size;
232
		size += highmem_size;
233 234 235 236
		for_each_populated_zone(zone) {
			tmp += snapshot_additional_pages(zone);
			if (is_highmem(zone)) {
				highmem_size -=
237
					zone_page_state(zone, NR_FREE_PAGES);
238 239 240
			} else {
				tmp -= zone_page_state(zone, NR_FREE_PAGES);
				tmp += zone->lowmem_reserve[ZONE_NORMAL];
241
			}
242
		}
243 244 245 246 247

		if (highmem_size < 0)
			highmem_size = 0;

		tmp += highmem_size;
248
		if (tmp > 0) {
249
			tmp = __shrink_memory(tmp);
250 251 252
			if (!tmp)
				return -ENOMEM;
			pages += tmp;
253
		} else if (size > image_size / PAGE_SIZE) {
254
			tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
255
			pages += tmp;
256 257 258
		}
		printk("\b%c", p[i++%4]);
	} while (tmp > 0);
259
	do_gettimeofday(&stop);
260
	printk("\bdone (%lu pages freed)\n", pages);
261
	swsusp_show_speed(&start, &stop, pages, "Freed");
262 263 264

	return 0;
}
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

/*
 * Platforms, like ACPI, may want us to save some memory used by them during
 * hibernation and to restore the contents of this memory during the subsequent
 * resume.  The code below implements a mechanism allowing us to do that.
 */

struct nvs_page {
	unsigned long phys_start;
	unsigned int size;
	void *kaddr;
	void *data;
	struct list_head node;
};

static LIST_HEAD(nvs_list);

/**
 *	hibernate_nvs_register - register platform NVS memory region to save
 *	@start - physical address of the region
 *	@size - size of the region
 *
 *	The NVS region need not be page-aligned (both ends) and we arrange
 *	things so that the data from page-aligned addresses in this region will
 *	be copied into separate RAM pages.
 */
int hibernate_nvs_register(unsigned long start, unsigned long size)
{
	struct nvs_page *entry, *next;

	while (size > 0) {
		unsigned int nr_bytes;

		entry = kzalloc(sizeof(struct nvs_page), GFP_KERNEL);
		if (!entry)
			goto Error;

		list_add_tail(&entry->node, &nvs_list);
		entry->phys_start = start;
		nr_bytes = PAGE_SIZE - (start & ~PAGE_MASK);
		entry->size = (size < nr_bytes) ? size : nr_bytes;

		start += entry->size;
		size -= entry->size;
	}
	return 0;

 Error:
	list_for_each_entry_safe(entry, next, &nvs_list, node) {
		list_del(&entry->node);
		kfree(entry);
	}
	return -ENOMEM;
}

/**
 *	hibernate_nvs_free - free data pages allocated for saving NVS regions
 */
void hibernate_nvs_free(void)
{
	struct nvs_page *entry;

	list_for_each_entry(entry, &nvs_list, node)
		if (entry->data) {
			free_page((unsigned long)entry->data);
			entry->data = NULL;
			if (entry->kaddr) {
				iounmap(entry->kaddr);
				entry->kaddr = NULL;
			}
		}
}

/**
 *	hibernate_nvs_alloc - allocate memory necessary for saving NVS regions
 */
int hibernate_nvs_alloc(void)
{
	struct nvs_page *entry;

	list_for_each_entry(entry, &nvs_list, node) {
		entry->data = (void *)__get_free_page(GFP_KERNEL);
		if (!entry->data) {
			hibernate_nvs_free();
			return -ENOMEM;
		}
	}
	return 0;
}

/**
 *	hibernate_nvs_save - save NVS memory regions
 */
void hibernate_nvs_save(void)
{
	struct nvs_page *entry;

	printk(KERN_INFO "PM: Saving platform NVS memory\n");

	list_for_each_entry(entry, &nvs_list, node)
		if (entry->data) {
			entry->kaddr = ioremap(entry->phys_start, entry->size);
			memcpy(entry->data, entry->kaddr, entry->size);
		}
}

/**
 *	hibernate_nvs_restore - restore NVS memory regions
 *
 *	This function is going to be called with interrupts disabled, so it
 *	cannot iounmap the virtual addresses used to access the NVS region.
 */
void hibernate_nvs_restore(void)
{
	struct nvs_page *entry;

	printk(KERN_INFO "PM: Restoring platform NVS memory\n");

	list_for_each_entry(entry, &nvs_list, node)
		if (entry->data)
			memcpy(entry->kaddr, entry->data, entry->size);
}