提交 f577eb30 编写于 作者: R Rafael J. Wysocki 提交者: Linus Torvalds

[PATCH] swsusp: low level interface

Introduce the low level interface that can be used for handling the
snapshot of the system memory by the in-kernel swap-writing/reading code of
swsusp and the userland interface code (to be introduced shortly).

Also change the way in which swsusp records the allocated swap pages and,
consequently, simplifies the in-kernel swap-writing/reading code (this is
necessary for the userland interface too).  To this end, it introduces two
helper functions in mm/swapfile.c, so that the swsusp code does not refer
directly to the swap internals.
Signed-off-by: NRafael J. Wysocki <rjw@sisk.pl>
Acked-by: NPavel Machek <pavel@ucw.cz>
Signed-off-by: NAdrian Bunk <bunk@stusta.de>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 2b322ce2
......@@ -234,14 +234,15 @@ extern struct page * read_swap_cache_async(swp_entry_t, struct vm_area_struct *v
/* linux/mm/swapfile.c */
extern long total_swap_pages;
extern unsigned int nr_swapfiles;
extern struct swap_info_struct swap_info[];
extern void si_swapinfo(struct sysinfo *);
extern swp_entry_t get_swap_page(void);
extern swp_entry_t get_swap_page_of_type(int type);
extern swp_entry_t get_swap_page_of_type(int);
extern int swap_duplicate(swp_entry_t);
extern int valid_swaphandles(swp_entry_t, unsigned long *);
extern void swap_free(swp_entry_t);
extern void free_swap_and_cache(swp_entry_t);
extern int swap_type_of(dev_t);
extern unsigned int count_swap_pages(int, int);
extern sector_t map_swap_page(struct swap_info_struct *, pgoff_t);
extern struct swap_info_struct *get_swap_info_struct(unsigned);
extern int can_share_swap_page(struct page *);
......
......@@ -26,9 +26,9 @@ extern suspend_disk_method_t pm_disk_mode;
extern int swsusp_shrink_memory(void);
extern int swsusp_suspend(void);
extern int swsusp_write(struct pbe *pblist, unsigned int nr_pages);
extern int swsusp_write(void);
extern int swsusp_check(void);
extern int swsusp_read(struct pbe **pblist_ptr);
extern int swsusp_read(void);
extern void swsusp_close(void);
extern int swsusp_resume(void);
......@@ -70,10 +70,6 @@ static void power_down(suspend_disk_method_t mode)
while(1);
}
static int in_suspend __nosavedata = 0;
static inline void platform_finish(void)
{
if (pm_disk_mode == PM_DISK_PLATFORM) {
......@@ -145,7 +141,7 @@ int pm_suspend_disk(void)
if (in_suspend) {
device_resume();
pr_debug("PM: writing image.\n");
error = swsusp_write(pagedir_nosave, nr_copy_pages);
error = swsusp_write();
if (!error)
power_down(pm_disk_mode);
else {
......@@ -216,7 +212,7 @@ static int software_resume(void)
pr_debug("PM: Reading swsusp image.\n");
if ((error = swsusp_read(&pagedir_nosave))) {
if ((error = swsusp_read())) {
swsusp_free();
goto Thaw;
}
......
......@@ -37,21 +37,31 @@ extern struct subsystem power_subsys;
/* References to section boundaries */
extern const void __nosave_begin, __nosave_end;
extern unsigned int nr_copy_pages;
extern struct pbe *pagedir_nosave;
/* Preferred image size in bytes (default 500 MB) */
extern unsigned long image_size;
extern int in_suspend;
extern asmlinkage int swsusp_arch_suspend(void);
extern asmlinkage int swsusp_arch_resume(void);
extern unsigned int count_data_pages(void);
extern void free_pagedir(struct pbe *pblist);
extern void release_eaten_pages(void);
extern struct pbe *alloc_pagedir(unsigned nr_pages, gfp_t gfp_mask, int safe_needed);
extern void swsusp_free(void);
extern int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed);
extern unsigned int snapshot_nr_pages(void);
extern struct pbe *snapshot_pblist(void);
extern void snapshot_pblist_set(struct pbe *pblist);
struct snapshot_handle {
loff_t offset;
unsigned int page;
unsigned int page_offset;
unsigned int prev;
struct pbe *pbe;
void *buffer;
unsigned int buf_offset;
};
#define data_of(handle) ((handle).buffer + (handle).buf_offset)
extern int snapshot_read_next(struct snapshot_handle *handle, size_t count);
extern int snapshot_write_next(struct snapshot_handle *handle, size_t count);
int snapshot_image_loaded(struct snapshot_handle *handle);
......@@ -10,6 +10,7 @@
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/suspend.h>
......@@ -34,7 +35,8 @@
#include "power.h"
struct pbe *pagedir_nosave;
unsigned int nr_copy_pages;
static unsigned int nr_copy_pages;
static unsigned int nr_meta_pages;
#ifdef CONFIG_HIGHMEM
unsigned int count_highmem_pages(void)
......@@ -235,7 +237,7 @@ static void copy_data_pages(struct pbe *pblist)
* free_pagedir - free pages allocated with alloc_pagedir()
*/
void free_pagedir(struct pbe *pblist)
static void free_pagedir(struct pbe *pblist)
{
struct pbe *pbe;
......@@ -301,7 +303,7 @@ struct eaten_page {
static struct eaten_page *eaten_pages = NULL;
void release_eaten_pages(void)
static void release_eaten_pages(void)
{
struct eaten_page *p, *q;
......@@ -376,7 +378,6 @@ struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed
if (!nr_pages)
return NULL;
pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
pblist = alloc_image_page(gfp_mask, safe_needed);
/* FIXME: rewrite this ugly loop */
for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
......@@ -414,6 +415,9 @@ void swsusp_free(void)
}
}
}
nr_copy_pages = 0;
nr_meta_pages = 0;
pagedir_nosave = NULL;
}
......@@ -437,7 +441,7 @@ static int enough_free_mem(unsigned int nr_pages)
(nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
}
int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
{
struct pbe *p;
......@@ -504,7 +508,319 @@ asmlinkage int swsusp_save(void)
*/
nr_copy_pages = nr_pages;
nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
return 0;
}
static void init_header(struct swsusp_info *info)
{
memset(info, 0, sizeof(struct swsusp_info));
info->version_code = LINUX_VERSION_CODE;
info->num_physpages = num_physpages;
memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
info->cpus = num_online_cpus();
info->image_pages = nr_copy_pages;
info->pages = nr_copy_pages + nr_meta_pages + 1;
}
/**
* pack_orig_addresses - the .orig_address fields of the PBEs from the
* list starting at @pbe are stored in the array @buf[] (1 page)
*/
static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
{
int j;
for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
buf[j] = pbe->orig_address;
pbe = pbe->next;
}
if (!pbe)
for (; j < PAGE_SIZE / sizeof(long); j++)
buf[j] = 0;
return pbe;
}
/**
* snapshot_read_next - used for reading the system memory snapshot.
*
* On the first call to it @handle should point to a zeroed
* snapshot_handle structure. The structure gets updated and a pointer
* to it should be passed to this function every next time.
*
* The @count parameter should contain the number of bytes the caller
* wants to read from the snapshot. It must not be zero.
*
* On success the function returns a positive number. Then, the caller
* is allowed to read up to the returned number of bytes from the memory
* location computed by the data_of() macro. The number returned
* may be smaller than @count, but this only happens if the read would
* cross a page boundary otherwise.
*
* The function returns 0 to indicate the end of data stream condition,
* and a negative number is returned on error. In such cases the
* structure pointed to by @handle is not updated and should not be used
* any more.
*/
int snapshot_read_next(struct snapshot_handle *handle, size_t count)
{
static unsigned long *buffer;
if (handle->page > nr_meta_pages + nr_copy_pages)
return 0;
if (!buffer) {
/* This makes the buffer be freed by swsusp_free() */
buffer = alloc_image_page(GFP_ATOMIC, 0);
if (!buffer)
return -ENOMEM;
}
if (!handle->offset) {
init_header((struct swsusp_info *)buffer);
handle->buffer = buffer;
handle->pbe = pagedir_nosave;
}
if (handle->prev < handle->page) {
if (handle->page <= nr_meta_pages) {
handle->pbe = pack_orig_addresses(buffer, handle->pbe);
if (!handle->pbe)
handle->pbe = pagedir_nosave;
} else {
handle->buffer = (void *)handle->pbe->address;
handle->pbe = handle->pbe->next;
}
handle->prev = handle->page;
}
handle->buf_offset = handle->page_offset;
if (handle->page_offset + count >= PAGE_SIZE) {
count = PAGE_SIZE - handle->page_offset;
handle->page_offset = 0;
handle->page++;
} else {
handle->page_offset += count;
}
handle->offset += count;
return count;
}
/**
* mark_unsafe_pages - mark the pages that cannot be used for storing
* the image during resume, because they conflict with the pages that
* had been used before suspend
*/
static int mark_unsafe_pages(struct pbe *pblist)
{
struct zone *zone;
unsigned long zone_pfn;
struct pbe *p;
if (!pblist) /* a sanity check */
return -EINVAL;
/* Clear page flags */
for_each_zone (zone) {
for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
if (pfn_valid(zone_pfn + zone->zone_start_pfn))
ClearPageNosaveFree(pfn_to_page(zone_pfn +
zone->zone_start_pfn));
}
/* Mark orig addresses */
for_each_pbe (p, pblist) {
if (virt_addr_valid(p->orig_address))
SetPageNosaveFree(virt_to_page(p->orig_address));
else
return -EFAULT;
}
return 0;
}
static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
{
/* We assume both lists contain the same number of elements */
while (src) {
dst->orig_address = src->orig_address;
dst = dst->next;
src = src->next;
}
}
static int check_header(struct swsusp_info *info)
{
char *reason = NULL;
if (info->version_code != LINUX_VERSION_CODE)
reason = "kernel version";
if (info->num_physpages != num_physpages)
reason = "memory size";
if (strcmp(info->uts.sysname,system_utsname.sysname))
reason = "system type";
if (strcmp(info->uts.release,system_utsname.release))
reason = "kernel release";
if (strcmp(info->uts.version,system_utsname.version))
reason = "version";
if (strcmp(info->uts.machine,system_utsname.machine))
reason = "machine";
if (reason) {
printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
return -EPERM;
}
return 0;
}
/**
* load header - check the image header and copy data from it
*/
static int load_header(struct snapshot_handle *handle,
struct swsusp_info *info)
{
int error;
struct pbe *pblist;
error = check_header(info);
if (!error) {
pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
if (!pblist)
return -ENOMEM;
pagedir_nosave = pblist;
handle->pbe = pblist;
nr_copy_pages = info->image_pages;
nr_meta_pages = info->pages - info->image_pages - 1;
}
return error;
}
/**
* unpack_orig_addresses - copy the elements of @buf[] (1 page) to
* the PBEs in the list starting at @pbe
*/
static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
struct pbe *pbe)
{
int j;
for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
pbe->orig_address = buf[j];
pbe = pbe->next;
}
return pbe;
}
/**
* create_image - use metadata contained in the PBE list
* pointed to by pagedir_nosave to mark the pages that will
* be overwritten in the process of restoring the system
* memory state from the image and allocate memory for
* the image avoiding these pages
*/
static int create_image(struct snapshot_handle *handle)
{
int error = 0;
struct pbe *p, *pblist;
p = pagedir_nosave;
error = mark_unsafe_pages(p);
if (!error) {
pblist = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
if (pblist)
copy_page_backup_list(pblist, p);
free_pagedir(p);
if (!pblist)
error = -ENOMEM;
}
if (!error)
error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
if (!error) {
release_eaten_pages();
pagedir_nosave = pblist;
} else {
pagedir_nosave = NULL;
handle->pbe = NULL;
nr_copy_pages = 0;
nr_meta_pages = 0;
}
return error;
}
/**
* snapshot_write_next - used for writing the system memory snapshot.
*
* On the first call to it @handle should point to a zeroed
* snapshot_handle structure. The structure gets updated and a pointer
* to it should be passed to this function every next time.
*
* The @count parameter should contain the number of bytes the caller
* wants to write to the image. It must not be zero.
*
* On success the function returns a positive number. Then, the caller
* is allowed to write up to the returned number of bytes to the memory
* location computed by the data_of() macro. The number returned
* may be smaller than @count, but this only happens if the write would
* cross a page boundary otherwise.
*
* The function returns 0 to indicate the "end of file" condition,
* and a negative number is returned on error. In such cases the
* structure pointed to by @handle is not updated and should not be used
* any more.
*/
int snapshot_write_next(struct snapshot_handle *handle, size_t count)
{
static unsigned long *buffer;
int error = 0;
if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
return 0;
if (!buffer) {
/* This makes the buffer be freed by swsusp_free() */
buffer = alloc_image_page(GFP_ATOMIC, 0);
if (!buffer)
return -ENOMEM;
}
if (!handle->offset)
handle->buffer = buffer;
if (handle->prev < handle->page) {
if (!handle->prev) {
error = load_header(handle, (struct swsusp_info *)buffer);
if (error)
return error;
} else if (handle->prev <= nr_meta_pages) {
handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
if (!handle->pbe) {
error = create_image(handle);
if (error)
return error;
handle->pbe = pagedir_nosave;
handle->buffer = (void *)handle->pbe->address;
}
} else {
handle->pbe = handle->pbe->next;
handle->buffer = (void *)handle->pbe->address;
}
handle->prev = handle->page;
}
handle->buf_offset = handle->page_offset;
if (handle->page_offset + count >= PAGE_SIZE) {
count = PAGE_SIZE - handle->page_offset;
handle->page_offset = 0;
handle->page++;
} else {
handle->page_offset += count;
}
handle->offset += count;
return count;
}
int snapshot_image_loaded(struct snapshot_handle *handle)
{
return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
handle->page <= nr_meta_pages + nr_copy_pages);
}
此差异已折叠。
......@@ -45,7 +45,7 @@ static const char Unused_offset[] = "Unused swap offset entry ";
struct swap_list_t swap_list = {-1, -1};
struct swap_info_struct swap_info[MAX_SWAPFILES];
static struct swap_info_struct swap_info[MAX_SWAPFILES];
static DEFINE_MUTEX(swapon_mutex);
......@@ -417,6 +417,59 @@ void free_swap_and_cache(swp_entry_t entry)
}
}
#ifdef CONFIG_SOFTWARE_SUSPEND
/*
* Find the swap type that corresponds to given device (if any)
*
* This is needed for software suspend and is done in such a way that inode
* aliasing is allowed.
*/
int swap_type_of(dev_t device)
{
int i;
if (!device)
return -EINVAL;
spin_lock(&swap_lock);
for (i = 0; i < nr_swapfiles; i++) {
struct inode *inode;
if (!(swap_info[i].flags & SWP_WRITEOK))
continue;
inode = swap_info->swap_file->f_dentry->d_inode;
if (S_ISBLK(inode->i_mode) &&
device == MKDEV(imajor(inode), iminor(inode))) {
spin_unlock(&swap_lock);
return i;
}
}
spin_unlock(&swap_lock);
return -ENODEV;
}
/*
* Return either the total number of swap pages of given type, or the number
* of free pages of that type (depending on @free)
*
* This is needed for software suspend
*/
unsigned int count_swap_pages(int type, int free)
{
unsigned int n = 0;
if (type < nr_swapfiles) {
spin_lock(&swap_lock);
if (swap_info[type].flags & SWP_WRITEOK) {
n = swap_info[type].pages;
if (free)
n -= swap_info[type].inuse_pages;
}
spin_unlock(&swap_lock);
}
return n;
}
#endif
/*
* No need to decide whether this PTE shares the swap entry with others,
* just let do_wp_page work it out if a write is requested later - to
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册