blob: 457084f500107fb7486173244cf8055814e7b78c [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>
34 * Added the swap map data structure and reworked the handling of swap
35 *
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
41#include <linux/module.h>
42#include <linux/mm.h>
43#include <linux/suspend.h>
44#include <linux/smp_lock.h>
45#include <linux/file.h>
46#include <linux/utsname.h>
47#include <linux/version.h>
48#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/spinlock.h>
51#include <linux/genhd.h>
52#include <linux/kernel.h>
53#include <linux/major.h>
54#include <linux/swap.h>
55#include <linux/pm.h>
56#include <linux/device.h>
57#include <linux/buffer_head.h>
58#include <linux/swapops.h>
59#include <linux/bootmem.h>
60#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/highmem.h>
62#include <linux/bio.h>
63
64#include <asm/uaccess.h>
65#include <asm/mmu_context.h>
66#include <asm/pgtable.h>
67#include <asm/tlbflush.h>
68#include <asm/io.h>
69
70#include "power.h"
71
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080072/*
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080073 * Preferred image size in bytes (tunable via /sys/power/image_size).
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080074 * When it is set to N, swsusp will do its best to ensure the image
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080075 * size will not exceed N bytes, but if that is impossible, it will
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080076 * try to create the smallest image possible.
77 */
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080078unsigned long image_size = 500 * 1024 * 1024;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -080079
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080080int in_suspend __nosavedata = 0;
81
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -080082#ifdef CONFIG_HIGHMEM
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080083unsigned int count_highmem_pages(void);
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -080084int save_highmem(void);
85int restore_highmem(void);
86#else
87static int save_highmem(void) { return 0; }
88static int restore_highmem(void) { return 0; }
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080089static unsigned int count_highmem_pages(void) { return 0; }
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -080090#endif
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092extern char resume_file[];
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define SWSUSP_SIG "S1SUSPEND"
95
96static struct swsusp_header {
Rafael J. Wysockif2d97f02006-01-06 00:12:24 -080097 char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -080098 swp_entry_t image;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 char orig_sig[10];
100 char sig[10];
101} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Saving part...
105 */
106
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800107static unsigned short root_swap = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800109static int mark_swapfiles(swp_entry_t start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int error;
112
Pavel Machek2e4d5822005-06-25 14:55:12 -0700113 rw_swap_page_sync(READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 swp_entry(root_swap, 0),
115 virt_to_page((unsigned long)&swsusp_header));
116 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
117 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
118 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
119 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800120 swsusp_header.image = start;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700121 error = rw_swap_page_sync(WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 swp_entry(root_swap, 0),
123 virt_to_page((unsigned long)
124 &swsusp_header));
125 } else {
126 pr_debug("swsusp: Partition is not swap space.\n");
127 error = -ENODEV;
128 }
129 return error;
130}
131
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800132/**
133 * swsusp_swap_check - check if the resume device is a swap device
134 * and get its index (if so)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static int swsusp_swap_check(void) /* This is called before saving image */
138{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800139 int res = swap_type_of(swsusp_resume_device);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700140
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800141 if (res >= 0) {
142 root_swap = res;
143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800145 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/**
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800149 * The bitmap is used for tracing allocated swap pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800151 * The entire bitmap consists of a number of bitmap_page
152 * structures linked with the help of the .next member.
153 * Thus each page can be allocated individually, so we only
154 * need to make 0-order memory allocations to create
155 * the bitmap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800157
158#define BITMAP_PAGE_SIZE (PAGE_SIZE - sizeof(void *))
159#define BITMAP_PAGE_CHUNKS (BITMAP_PAGE_SIZE / sizeof(long))
160#define BITS_PER_CHUNK (sizeof(long) * 8)
161#define BITMAP_PAGE_BITS (BITMAP_PAGE_CHUNKS * BITS_PER_CHUNK)
162
163struct bitmap_page {
164 unsigned long chunks[BITMAP_PAGE_CHUNKS];
165 struct bitmap_page *next;
166};
167
168/**
169 * The following functions are used for tracing the allocated
170 * swap pages, so that they can be freed in case of an error.
171 *
172 * The functions operate on a linked bitmap structure defined
173 * above
174 */
175
176static void free_bitmap(struct bitmap_page *bitmap)
177{
178 struct bitmap_page *bp;
179
180 while (bitmap) {
181 bp = bitmap->next;
182 free_page((unsigned long)bitmap);
183 bitmap = bp;
184 }
185}
186
187static struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
188{
189 struct bitmap_page *bitmap, *bp;
190 unsigned int n;
191
192 if (!nr_bits)
193 return NULL;
194
195 bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
196 bp = bitmap;
197 for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
198 bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
199 bp = bp->next;
200 if (!bp) {
201 free_bitmap(bitmap);
202 return NULL;
203 }
204 }
205 return bitmap;
206}
207
208static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
209{
210 unsigned int n;
211
212 n = BITMAP_PAGE_BITS;
213 while (bitmap && n <= bit) {
214 n += BITMAP_PAGE_BITS;
215 bitmap = bitmap->next;
216 }
217 if (!bitmap)
218 return -EINVAL;
219 n -= BITMAP_PAGE_BITS;
220 bit -= n;
221 n = 0;
222 while (bit >= BITS_PER_CHUNK) {
223 bit -= BITS_PER_CHUNK;
224 n++;
225 }
226 bitmap->chunks[n] |= (1UL << bit);
227 return 0;
228}
229
230static unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
231{
232 unsigned long offset;
233
234 offset = swp_offset(get_swap_page_of_type(swap));
235 if (offset) {
236 if (bitmap_set(bitmap, offset)) {
237 swap_free(swp_entry(swap, offset));
238 offset = 0;
239 }
240 }
241 return offset;
242}
243
244static void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
245{
246 unsigned int bit, n;
247 unsigned long test;
248
249 bit = 0;
250 while (bitmap) {
251 for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
252 for (test = 1UL; test; test <<= 1) {
253 if (bitmap->chunks[n] & test)
254 swap_free(swp_entry(swap, bit));
255 bit++;
256 }
257 bitmap = bitmap->next;
258 }
259}
260
261/**
262 * write_page - Write one page to given swap location.
263 * @buf: Address we're writing.
264 * @offset: Offset of the swap page we're writing to.
265 */
266
267static int write_page(void *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 swp_entry_t entry;
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800270 int error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800272 if (offset) {
273 entry = swp_entry(root_swap, offset);
274 error = rw_swap_page_sync(WRITE, entry, virt_to_page(buf));
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return error;
277}
278
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800279/*
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800280 * The swap map is a data structure used for keeping track of each page
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800281 * written to a swap partition. It consists of many swap_map_page
282 * structures that contain each an array of MAP_PAGE_SIZE swap entries.
283 * These structures are stored on the swap and linked together with the
284 * help of the .next_swap member.
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800285 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800286 * The swap map is created during suspend. The swap map pages are
287 * allocated and populated one at a time, so we only need one memory
288 * page to set up the entire structure.
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800289 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800290 * During resume we also only need to use one swap_map_page structure
291 * at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800294#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800295
296struct swap_map_page {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800297 unsigned long entries[MAP_PAGE_ENTRIES];
298 unsigned long next_swap;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800299};
300
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800301/**
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800302 * The swap_map_handle structure is used for handling swap in
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800303 * a file-alike way
304 */
305
306struct swap_map_handle {
307 struct swap_map_page *cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800308 unsigned long cur_swap;
309 struct bitmap_page *bitmap;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800310 unsigned int k;
311};
312
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800313static void release_swap_writer(struct swap_map_handle *handle)
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800314{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800315 if (handle->cur)
316 free_page((unsigned long)handle->cur);
317 handle->cur = NULL;
318 if (handle->bitmap)
319 free_bitmap(handle->bitmap);
320 handle->bitmap = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800321}
322
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800323static int get_swap_writer(struct swap_map_handle *handle)
324{
325 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
326 if (!handle->cur)
327 return -ENOMEM;
328 handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0));
329 if (!handle->bitmap) {
330 release_swap_writer(handle);
331 return -ENOMEM;
332 }
333 handle->cur_swap = alloc_swap_page(root_swap, handle->bitmap);
334 if (!handle->cur_swap) {
335 release_swap_writer(handle);
336 return -ENOSPC;
337 }
338 handle->k = 0;
339 return 0;
340}
341
342static int swap_write_page(struct swap_map_handle *handle, void *buf)
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800343{
344 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800345 unsigned long offset;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800346
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800347 if (!handle->cur)
348 return -EINVAL;
349 offset = alloc_swap_page(root_swap, handle->bitmap);
350 error = write_page(buf, offset);
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800351 if (error)
352 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800353 handle->cur->entries[handle->k++] = offset;
354 if (handle->k >= MAP_PAGE_ENTRIES) {
355 offset = alloc_swap_page(root_swap, handle->bitmap);
356 if (!offset)
357 return -ENOSPC;
358 handle->cur->next_swap = offset;
359 error = write_page(handle->cur, handle->cur_swap);
360 if (error)
361 return error;
362 memset(handle->cur, 0, PAGE_SIZE);
363 handle->cur_swap = offset;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800364 handle->k = 0;
365 }
366 return 0;
367}
368
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800369static int flush_swap_writer(struct swap_map_handle *handle)
370{
371 if (handle->cur && handle->cur_swap)
372 return write_page(handle->cur, handle->cur_swap);
373 else
374 return -EINVAL;
375}
376
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800377/**
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800378 * save_image - save the suspend image data
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800379 */
380
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800381static int save_image(struct swap_map_handle *handle,
382 struct snapshot_handle *snapshot,
383 unsigned int nr_pages)
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800384{
385 unsigned int m;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800386 int ret;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800387 int error = 0;
388
389 printk("Saving image data pages (%u pages) ... ", nr_pages);
390 m = nr_pages / 100;
391 if (!m)
392 m = 1;
393 nr_pages = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800394 do {
395 ret = snapshot_read_next(snapshot, PAGE_SIZE);
396 if (ret > 0) {
397 error = swap_write_page(handle, data_of(*snapshot));
398 if (error)
399 break;
400 if (!(nr_pages % m))
401 printk("\b\b\b\b%3d%%", nr_pages / m);
402 nr_pages++;
403 }
404 } while (ret > 0);
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800405 if (!error)
406 printk("\b\b\b\bdone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return error;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800411 * enough_swap - Make sure we have enough swap to save the image.
412 *
413 * Returns TRUE or FALSE after checking the total amount of swap
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800414 * space avaiable from the resume partition.
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800415 */
416
417static int enough_swap(unsigned int nr_pages)
418{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800419 unsigned int free_swap = count_swap_pages(root_swap, 1);
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800420
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800421 pr_debug("swsusp: free swap pages: %u\n", free_swap);
422 return free_swap > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800423 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
424}
425
426/**
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800427 * swsusp_write - Write entire image and metadata.
428 *
429 * It is important _NOT_ to umount filesystems at this point. We want
430 * them synced (in case something goes wrong) but we DO not want to mark
431 * filesystem clean: it is not. (And it does not matter, if we resume
432 * correctly, we'll mark system clean, anyway.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800434
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800435int swsusp_write(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800437 struct swap_map_handle handle;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800438 struct snapshot_handle snapshot;
439 struct swsusp_info *header;
440 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int error;
442
Rafael J. Wysocki1adf6c82006-01-06 00:17:16 -0800443 if ((error = swsusp_swap_check())) {
444 printk(KERN_ERR "swsusp: Cannot find swap device, try swapon -a.\n");
445 return error;
446 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800447 memset(&snapshot, 0, sizeof(struct snapshot_handle));
448 error = snapshot_read_next(&snapshot, PAGE_SIZE);
449 if (error < PAGE_SIZE)
450 return error < 0 ? error : -EFAULT;
451 header = (struct swsusp_info *)data_of(snapshot);
452 if (!enough_swap(header->pages)) {
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800453 printk(KERN_ERR "swsusp: Not enough free swap\n");
454 return -ENOSPC;
455 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800456 error = get_swap_writer(&handle);
457 if (!error) {
458 start = handle.cur_swap;
459 error = swap_write_page(&handle, header);
460 }
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800461 if (!error)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800462 error = save_image(&handle, &snapshot, header->pages - 1);
463 if (!error) {
464 flush_swap_writer(&handle);
465 printk("S");
466 error = mark_swapfiles(swp_entry(root_swap, start));
467 printk("|\n");
468 }
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800469 if (error)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800470 free_all_swap_pages(root_swap, handle.bitmap);
471 release_swap_writer(&handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800475/**
476 * swsusp_shrink_memory - Try to free as much memory as needed
477 *
478 * ... but do not OOM-kill anyone
479 *
480 * Notice: all userland should be stopped before it is called, or
481 * livelock is possible.
482 */
483
484#define SHRINK_BITE 10000
485
486int swsusp_shrink_memory(void)
487{
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800488 long size, tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800489 struct zone *zone;
490 unsigned long pages = 0;
491 unsigned int i = 0;
492 char *p = "-\\|/";
493
494 printk("Shrinking memory... ");
495 do {
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800496 size = 2 * count_highmem_pages();
497 size += size / 50 + count_data_pages();
498 size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800499 PAGES_FOR_IO;
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800500 tmp = size;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800501 for_each_zone (zone)
502 if (!is_highmem(zone))
503 tmp -= zone->free_pages;
504 if (tmp > 0) {
505 tmp = shrink_all_memory(SHRINK_BITE);
506 if (!tmp)
507 return -ENOMEM;
508 pages += tmp;
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800509 } else if (size > image_size / PAGE_SIZE) {
Rafael J. Wysockib3a93a22006-01-06 00:15:22 -0800510 tmp = shrink_all_memory(SHRINK_BITE);
511 pages += tmp;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800512 }
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800513 printk("\b%c", p[i++%4]);
514 } while (tmp > 0);
515 printk("\bdone (%lu pages freed)\n", pages);
516
517 return 0;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520int swsusp_suspend(void)
521{
522 int error;
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if ((error = arch_prepare_suspend()))
525 return error;
526 local_irq_disable();
527 /* At this point, device_suspend() has been called, but *not*
528 * device_power_down(). We *must* device_power_down() now.
529 * Otherwise, drivers for some devices (e.g. interrupt controllers)
530 * become desynchronized with the actual state of the hardware
531 * at resume time, and evil weirdness ensues.
532 */
533 if ((error = device_power_down(PMSG_FREEZE))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700534 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800535 goto Enable_irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700537
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800538 if ((error = save_highmem())) {
539 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
540 goto Restore_highmem;
Pavel Machek47b724f2005-07-07 17:56:44 -0700541 }
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 save_processor_state();
544 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700545 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Restore control flow magically appears here */
547 restore_processor_state();
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800548Restore_highmem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 restore_highmem();
550 device_power_up();
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800551Enable_irqs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 local_irq_enable();
553 return error;
554}
555
556int swsusp_resume(void)
557{
558 int error;
559 local_irq_disable();
560 if (device_power_down(PMSG_FREEZE))
561 printk(KERN_ERR "Some devices failed to power down, very bad\n");
562 /* We'll ignore saved state, but this gets preempt count (etc) right */
563 save_processor_state();
564 error = swsusp_arch_resume();
565 /* Code below is only ever reached in case of failure. Otherwise
566 * execution continues at place where swsusp_arch_suspend was called
567 */
568 BUG_ON(!error);
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800569 /* The only reason why swsusp_arch_resume() can fail is memory being
570 * very tight, so we have to free it as soon as we can to avoid
571 * subsequent failures
572 */
573 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 restore_processor_state();
575 restore_highmem();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700576 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 device_power_up();
578 local_irq_enable();
579 return error;
580}
581
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700582/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * Using bio to read from swap.
584 * This code requires a bit more work than just using buffer heads
585 * but, it is the recommended way for 2.5/2.6.
586 * The following are to signal the beginning and end of I/O. Bios
587 * finish asynchronously, while we want them to happen synchronously.
588 * A simple atomic_t, and a wait loop take care of this problem.
589 */
590
591static atomic_t io_done = ATOMIC_INIT(0);
592
Pavel Machekdc19d502005-11-07 00:58:40 -0800593static int end_io(struct bio *bio, unsigned int num, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
596 panic("I/O error reading memory image");
597 atomic_set(&io_done, 0);
598 return 0;
599}
600
Pavel Machekdc19d502005-11-07 00:58:40 -0800601static struct block_device *resume_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/**
604 * submit - submit BIO request.
605 * @rw: READ or WRITE.
606 * @off physical offset of page.
607 * @page: page we're reading or writing.
608 *
609 * Straight from the textbook - allocate and initialize the bio.
610 * If we're writing, make sure the page is marked as dirty.
611 * Then submit it and wait.
612 */
613
Pavel Machekdc19d502005-11-07 00:58:40 -0800614static int submit(int rw, pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800617 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 bio = bio_alloc(GFP_ATOMIC, 1);
620 if (!bio)
621 return -ENOMEM;
622 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 bio->bi_bdev = resume_bdev;
624 bio->bi_end_io = end_io;
625
626 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
627 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
628 error = -EFAULT;
629 goto Done;
630 }
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 atomic_set(&io_done, 1);
634 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
635 while (atomic_read(&io_done))
636 yield();
Pavel Machek7714d592006-02-07 12:58:22 -0800637 if (rw == READ)
638 bio_set_pages_dirty(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 Done:
640 bio_put(bio);
641 return error;
642}
643
Pavel Machekdc19d502005-11-07 00:58:40 -0800644static int bio_read_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 return submit(READ, page_off, page);
647}
648
Pavel Machekdc19d502005-11-07 00:58:40 -0800649static int bio_write_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 return submit(WRITE, page_off, page);
652}
653
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800654/**
655 * The following functions allow us to read data using a swap map
656 * in a file-alike way
657 */
658
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800659static void release_swap_reader(struct swap_map_handle *handle)
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800660{
661 if (handle->cur)
662 free_page((unsigned long)handle->cur);
663 handle->cur = NULL;
664}
665
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800666static int get_swap_reader(struct swap_map_handle *handle,
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800667 swp_entry_t start)
668{
669 int error;
670
671 if (!swp_offset(start))
672 return -EINVAL;
673 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_ATOMIC);
674 if (!handle->cur)
675 return -ENOMEM;
676 error = bio_read_page(swp_offset(start), handle->cur);
677 if (error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800678 release_swap_reader(handle);
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800679 return error;
680 }
681 handle->k = 0;
682 return 0;
683}
684
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800685static int swap_read_page(struct swap_map_handle *handle, void *buf)
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800686{
687 unsigned long offset;
688 int error;
689
690 if (!handle->cur)
691 return -EINVAL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800692 offset = handle->cur->entries[handle->k];
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800693 if (!offset)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800694 return -EFAULT;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800695 error = bio_read_page(offset, buf);
696 if (error)
697 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800698 if (++handle->k >= MAP_PAGE_ENTRIES) {
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800699 handle->k = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800700 offset = handle->cur->next_swap;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800701 if (!offset)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800702 release_swap_reader(handle);
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800703 else
704 error = bio_read_page(offset, handle->cur);
705 }
706 return error;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/**
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800710 * load_image - load the image using the swap map handle
711 * @handle and the snapshot handle @snapshot
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800712 * (assume there are @nr_pages pages to load)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
714
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800715static int load_image(struct swap_map_handle *handle,
716 struct snapshot_handle *snapshot,
717 unsigned int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800719 unsigned int m;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800720 int ret;
721 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800723 printk("Loading image data pages (%u pages) ... ", nr_pages);
724 m = nr_pages / 100;
725 if (!m)
726 m = 1;
727 nr_pages = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800728 do {
729 ret = snapshot_write_next(snapshot, PAGE_SIZE);
730 if (ret > 0) {
731 error = swap_read_page(handle, data_of(*snapshot));
732 if (error)
733 break;
734 if (!(nr_pages % m))
735 printk("\b\b\b\b%3d%%", nr_pages / m);
736 nr_pages++;
737 }
738 } while (ret > 0);
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800739 if (!error)
740 printk("\b\b\b\bdone\n");
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800741 if (!snapshot_image_loaded(snapshot))
742 error = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return error;
744}
745
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800746int swsusp_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800748 int error;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800749 struct swap_map_handle handle;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800750 struct snapshot_handle snapshot;
751 struct swsusp_info *header;
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800752 unsigned int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800754 if (IS_ERR(resume_bdev)) {
755 pr_debug("swsusp: block device not initialised\n");
756 return PTR_ERR(resume_bdev);
757 }
758
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800759 memset(&snapshot, 0, sizeof(struct snapshot_handle));
760 error = snapshot_write_next(&snapshot, PAGE_SIZE);
761 if (error < PAGE_SIZE)
762 return error < 0 ? error : -EFAULT;
763 header = (struct swsusp_info *)data_of(snapshot);
764 error = get_swap_reader(&handle, swsusp_header.image);
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800765 if (!error)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800766 error = swap_read_page(&handle, header);
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800767 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800768 nr_pages = header->image_pages;
769 error = load_image(&handle, &snapshot, nr_pages);
Rafael J. Wysockied14b522005-11-08 21:34:40 -0800770 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800771 release_swap_reader(&handle);
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800772
773 blkdev_put(resume_bdev);
774
775 if (!error)
776 pr_debug("swsusp: Reading resume file was successful\n");
777 else
778 pr_debug("swsusp: Error %d resuming\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return error;
780}
781
782/**
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800783 * swsusp_check - Check for swsusp signature in the resume device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785
786int swsusp_check(void)
787{
788 int error;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
791 if (!IS_ERR(resume_bdev)) {
792 set_blocksize(resume_bdev, PAGE_SIZE);
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800793 memset(&swsusp_header, 0, sizeof(swsusp_header));
794 if ((error = bio_read_page(0, &swsusp_header)))
795 return error;
796 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
797 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
798 /* Reset swap signature now */
799 error = bio_write_page(0, &swsusp_header);
800 } else {
801 return -EINVAL;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (error)
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800804 blkdev_put(resume_bdev);
805 else
806 pr_debug("swsusp: Signature found, resuming\n");
807 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 error = PTR_ERR(resume_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Rafael J. Wysocki277c6e22006-01-06 00:17:58 -0800811 if (error)
812 pr_debug("swsusp: Error %d check for resume file\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return error;
815}
816
817/**
818 * swsusp_close - close swap device.
819 */
820
821void swsusp_close(void)
822{
823 if (IS_ERR(resume_bdev)) {
824 pr_debug("swsusp: block device not initialised\n");
825 return;
826 }
827
828 blkdev_put(resume_bdev);
829}