2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* linux/kernel/power/swsusp.c
|
|
|
|
*
|
2005-10-30 15:59:58 -07:00
|
|
|
* This file provides code to write suspend image to swap and read it back.
|
2005-04-16 16:20:36 -06:00
|
|
|
*
|
|
|
|
* Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
|
2005-10-30 15:59:56 -07:00
|
|
|
* Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
|
2005-04-16 16:20:36 -06:00
|
|
|
*
|
|
|
|
* This file is released under the GPLv2.
|
|
|
|
*
|
|
|
|
* I'd like to thank the following people for their work:
|
2005-06-25 15:55:12 -06:00
|
|
|
*
|
2005-04-16 16:20:36 -06:00
|
|
|
* 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.
|
|
|
|
*
|
2005-06-25 15:55:12 -06:00
|
|
|
* Steve Doddi <dirk@loth.demon.co.uk>:
|
2005-04-16 16:20:36 -06:00
|
|
|
* 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
|
|
|
|
*
|
2006-01-06 01:13:05 -07:00
|
|
|
* Rafael J. Wysocki <rjw@sisk.pl>
|
2006-03-23 04:00:00 -07:00
|
|
|
* Reworked the freeing of memory and the handling of swap
|
2006-01-06 01:13:05 -07:00
|
|
|
*
|
2005-04-16 16:20:36 -06:00
|
|
|
* 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"
|
|
|
|
|
2006-01-06 01:15:56 -07:00
|
|
|
/*
|
2006-02-01 04:05:07 -07:00
|
|
|
* Preferred image size in bytes (tunable via /sys/power/image_size).
|
2006-01-06 01:15:56 -07:00
|
|
|
* When it is set to N, swsusp will do its best to ensure the image
|
2006-02-01 04:05:07 -07:00
|
|
|
* size will not exceed N bytes, but if that is impossible, it will
|
2006-01-06 01:15:56 -07:00
|
|
|
* try to create the smallest image possible.
|
|
|
|
*/
|
2006-02-01 04:05:07 -07:00
|
|
|
unsigned long image_size = 500 * 1024 * 1024;
|
2006-01-06 01:15:56 -07:00
|
|
|
|
2006-03-23 03:59:59 -07:00
|
|
|
int in_suspend __nosavedata = 0;
|
|
|
|
|
2006-06-25 19:41:00 -06:00
|
|
|
#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
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/**
|
2006-03-23 03:59:59 -07:00
|
|
|
* The following functions are used for tracing the allocated
|
|
|
|
* swap pages, so that they can be freed in case of an error.
|
2006-01-06 01:13:05 -07:00
|
|
|
*
|
2006-03-23 03:59:59 -07:00
|
|
|
* The functions operate on a linked bitmap structure defined
|
2006-03-23 04:00:00 -07:00
|
|
|
* in power.h
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
2006-01-06 01:13:05 -07:00
|
|
|
|
2006-03-23 04:00:00 -07:00
|
|
|
void free_bitmap(struct bitmap_page *bitmap)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-03-23 03:59:59 -07:00
|
|
|
struct bitmap_page *bp;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-03-23 03:59:59 -07:00
|
|
|
while (bitmap) {
|
|
|
|
bp = bitmap->next;
|
|
|
|
free_page((unsigned long)bitmap);
|
|
|
|
bitmap = bp;
|
2006-01-06 01:13:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 04:00:00 -07:00
|
|
|
struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
|
2006-01-06 01:13:05 -07:00
|
|
|
{
|
2006-03-23 03:59:59 -07:00
|
|
|
struct bitmap_page *bitmap, *bp;
|
|
|
|
unsigned int n;
|
2006-01-06 01:13:05 -07:00
|
|
|
|
2006-03-23 03:59:59 -07:00
|
|
|
if (!nr_bits)
|
2006-01-06 01:13:05 -07:00
|
|
|
return NULL;
|
|
|
|
|
2006-03-23 03:59:59 -07:00
|
|
|
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);
|
2006-01-06 01:13:05 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-03-23 03:59:59 -07:00
|
|
|
return bitmap;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-03-23 03:59:59 -07:00
|
|
|
static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-03-23 03:59:59 -07:00
|
|
|
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++;
|
2006-01-06 01:13:05 -07:00
|
|
|
}
|
2006-03-23 03:59:59 -07:00
|
|
|
bitmap->chunks[n] |= (1UL << bit);
|
|
|
|
return 0;
|
2006-01-06 01:13:05 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-12-06 21:34:10 -07:00
|
|
|
sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap)
|
2006-01-06 01:13:05 -07:00
|
|
|
{
|
2006-03-23 03:59:59 -07:00
|
|
|
unsigned long offset;
|
|
|
|
|
|
|
|
offset = swp_offset(get_swap_page_of_type(swap));
|
|
|
|
if (offset) {
|
2006-12-06 21:34:10 -07:00
|
|
|
if (bitmap_set(bitmap, offset))
|
2006-03-23 03:59:59 -07:00
|
|
|
swap_free(swp_entry(swap, offset));
|
2006-12-06 21:34:10 -07:00
|
|
|
else
|
|
|
|
return swapdev_block(swap, offset);
|
2006-01-06 01:13:05 -07:00
|
|
|
}
|
2006-12-06 21:34:10 -07:00
|
|
|
return 0;
|
2006-01-06 01:13:05 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-03-23 04:00:00 -07:00
|
|
|
void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
|
2006-01-06 01:13:05 -07:00
|
|
|
{
|
2006-03-23 03:59:59 -07:00
|
|
|
unsigned int bit, n;
|
|
|
|
unsigned long test;
|
2006-01-06 01:13:05 -07:00
|
|
|
|
2006-03-23 03:59:59 -07:00
|
|
|
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;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-01-06 01:13:05 -07:00
|
|
|
}
|
|
|
|
|
2006-01-06 01:13:46 -07:00
|
|
|
/**
|
|
|
|
* 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
|
2006-06-23 03:03:18 -06:00
|
|
|
static inline unsigned long __shrink_memory(long tmp)
|
|
|
|
{
|
|
|
|
if (tmp > SHRINK_BITE)
|
|
|
|
tmp = SHRINK_BITE;
|
|
|
|
return shrink_all_memory(tmp);
|
|
|
|
}
|
2006-01-06 01:13:46 -07:00
|
|
|
|
|
|
|
int swsusp_shrink_memory(void)
|
|
|
|
{
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
long tmp;
|
2006-01-06 01:13:46 -07:00
|
|
|
struct zone *zone;
|
|
|
|
unsigned long pages = 0;
|
|
|
|
unsigned int i = 0;
|
|
|
|
char *p = "-\\|/";
|
|
|
|
|
|
|
|
printk("Shrinking memory... ");
|
|
|
|
do {
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
long size, highmem_size;
|
|
|
|
|
|
|
|
highmem_size = count_highmem_pages();
|
|
|
|
size = count_data_pages() + PAGES_FOR_IO;
|
2006-01-06 01:15:22 -07:00
|
|
|
tmp = size;
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
size += highmem_size;
|
2006-01-06 01:13:46 -07:00
|
|
|
for_each_zone (zone)
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
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);
|
|
|
|
}
|
2006-06-23 03:04:46 -06:00
|
|
|
}
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
|
|
|
|
if (highmem_size < 0)
|
|
|
|
highmem_size = 0;
|
|
|
|
|
|
|
|
tmp += highmem_size;
|
2006-01-06 01:13:46 -07:00
|
|
|
if (tmp > 0) {
|
2006-06-23 03:03:18 -06:00
|
|
|
tmp = __shrink_memory(tmp);
|
2006-01-06 01:13:46 -07:00
|
|
|
if (!tmp)
|
|
|
|
return -ENOMEM;
|
|
|
|
pages += tmp;
|
2006-02-01 04:05:07 -07:00
|
|
|
} else if (size > image_size / PAGE_SIZE) {
|
2006-06-23 03:03:18 -06:00
|
|
|
tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
|
2006-01-06 01:15:22 -07:00
|
|
|
pages += tmp;
|
2006-01-06 01:13:46 -07:00
|
|
|
}
|
|
|
|
printk("\b%c", p[i++%4]);
|
|
|
|
} while (tmp > 0);
|
|
|
|
printk("\bdone (%lu pages freed)\n", pages);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
int swsusp_suspend(void)
|
|
|
|
{
|
|
|
|
int error;
|
2005-11-08 22:34:41 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
if ((error = arch_prepare_suspend()))
|
|
|
|
return error;
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
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))) {
|
2005-09-03 16:57:05 -06:00
|
|
|
printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
|
2005-11-08 22:34:41 -07:00
|
|
|
goto Enable_irqs;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2005-07-07 18:56:44 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
save_processor_state();
|
|
|
|
if ((error = swsusp_arch_suspend()))
|
2005-09-03 16:57:05 -06:00
|
|
|
printk(KERN_ERR "Error %d suspending\n", error);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Restore control flow magically appears here */
|
|
|
|
restore_processor_state();
|
2006-08-15 00:11:08 -06:00
|
|
|
/* NOTE: device_power_up() is just a resume() for devices
|
|
|
|
* that suspended with irqs off ... no overall powerup.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
device_power_up();
|
2005-11-08 22:34:41 -07:00
|
|
|
Enable_irqs:
|
2005-04-16 16:20:36 -06:00
|
|
|
local_irq_enable();
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int swsusp_resume(void)
|
|
|
|
{
|
|
|
|
int error;
|
2006-08-15 00:11:08 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
local_irq_disable();
|
2006-08-15 00:11:08 -06:00
|
|
|
/* 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))
|
2005-04-16 16:20:36 -06:00
|
|
|
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();
|
[PATCH] swsusp: Improve handling of highmem
Currently swsusp saves the contents of highmem pages by copying them to the
normal zone which is quite inefficient (eg. it requires two normal pages
to be used for saving one highmem page). This may be improved by using
highmem for saving the contents of saveable highmem pages.
Namely, during the suspend phase of the suspend-resume cycle we try to
allocate as many free highmem pages as there are saveable highmem pages.
If there are not enough highmem image pages to store the contents of all of
the saveable highmem pages, some of them will be stored in the "normal"
memory. Next, we allocate as many free "normal" pages as needed to store
the (remaining) image data. We use a memory bitmap to mark the allocated
free pages (ie. highmem as well as "normal" image pages).
Now, we use another memory bitmap to mark all of the saveable pages
(highmem as well as "normal") and the contents of the saveable pages are
copied into the image pages. Then, the second bitmap is used to save the
pfns corresponding to the saveable pages and the first one is used to save
their data.
During the resume phase the pfns of the pages that were saveable during the
suspend are loaded from the image and used to mark the "unsafe" page
frames. Next, we try to allocate as many free highmem page frames as to
load all of the image data that had been in the highmem before the suspend
and we allocate so many free "normal" page frames that the total number of
allocated free pages (highmem and "normal") is equal to the size of the
image. While doing this we have to make sure that there will be some extra
free "normal" and "safe" page frames for two lists of PBEs constructed
later.
Now, the image data are loaded, if possible, into their "original" page
frames. The image data that cannot be written into their "original" page
frames are loaded into "safe" page frames and their "original" kernel
virtual addresses, as well as the addresses of the "safe" pages containing
their copies, are stored in one of two lists of PBEs.
One list of PBEs is for the copies of "normal" suspend pages (ie. "normal"
pages that were saveable during the suspend) and it is used in the same way
as previously (ie. by the architecture-dependent parts of swsusp). The
other list of PBEs is for the copies of highmem suspend pages. The pages
in this list are restored (in a reversible way) right before the
arch-dependent code is called.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-12-06 21:34:18 -07:00
|
|
|
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();
|
|
|
|
}
|
2005-10-30 15:59:58 -07:00
|
|
|
/* 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();
|
2005-04-16 16:20:36 -06:00
|
|
|
restore_processor_state();
|
2005-09-06 16:16:27 -06:00
|
|
|
touch_softlockup_watchdog();
|
2005-04-16 16:20:36 -06:00
|
|
|
device_power_up();
|
|
|
|
local_irq_enable();
|
|
|
|
return error;
|
|
|
|
}
|