Commit d1d241cc authored by Rafael J. Wysocki's avatar Rafael J. Wysocki Committed by Linus Torvalds

swsusp: use rbtree for tracking allocated swap

Make swsusp use extents instead of a bitmap to trace swap pages allocated
for saving the image (the tracking is only needed in case there's an error,
so that the allocated swap pages can be released).

This should allow us to reduce the memory usage, practically always, and
improve performance.
Signed-off-by: default avatarRafael J. Wysocki <rjw@sisk.pl>
Cc: Nigel Cunningham <nigel@nigel.suspend2.net>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 726162b5
...@@ -143,30 +143,9 @@ struct resume_swap_area { ...@@ -143,30 +143,9 @@ struct resume_swap_area {
/* If unset, the snapshot device cannot be open. */ /* If unset, the snapshot device cannot be open. */
extern atomic_t snapshot_device_available; extern atomic_t snapshot_device_available;
/** extern sector_t alloc_swapdev_block(int swap);
* The bitmap is used for tracing allocated swap pages extern void free_all_swap_pages(int swap);
* extern int swsusp_swap_in_use(void);
* The entire bitmap consists of a number of bitmap_page
* structures linked with the help of the .next member.
* Thus each page can be allocated individually, so we only
* need to make 0-order memory allocations to create
* the bitmap.
*/
#define BITMAP_PAGE_SIZE (PAGE_SIZE - sizeof(void *))
#define BITMAP_PAGE_CHUNKS (BITMAP_PAGE_SIZE / sizeof(long))
#define BITS_PER_CHUNK (sizeof(long) * 8)
#define BITMAP_PAGE_BITS (BITMAP_PAGE_CHUNKS * BITS_PER_CHUNK)
struct bitmap_page {
unsigned long chunks[BITMAP_PAGE_CHUNKS];
struct bitmap_page *next;
};
extern void free_bitmap(struct bitmap_page *bitmap);
extern struct bitmap_page *alloc_bitmap(unsigned int nr_bits);
extern sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap);
extern void free_all_swap_pages(int swap, struct bitmap_page *bitmap);
extern int swsusp_check(void); extern int swsusp_check(void);
extern int swsusp_shrink_memory(void); extern int swsusp_shrink_memory(void);
......
...@@ -243,7 +243,6 @@ struct swap_map_page { ...@@ -243,7 +243,6 @@ struct swap_map_page {
struct swap_map_handle { struct swap_map_handle {
struct swap_map_page *cur; struct swap_map_page *cur;
sector_t cur_swap; sector_t cur_swap;
struct bitmap_page *bitmap;
unsigned int k; unsigned int k;
}; };
...@@ -252,9 +251,6 @@ static void release_swap_writer(struct swap_map_handle *handle) ...@@ -252,9 +251,6 @@ static void release_swap_writer(struct swap_map_handle *handle)
if (handle->cur) if (handle->cur)
free_page((unsigned long)handle->cur); free_page((unsigned long)handle->cur);
handle->cur = NULL; handle->cur = NULL;
if (handle->bitmap)
free_bitmap(handle->bitmap);
handle->bitmap = NULL;
} }
static int get_swap_writer(struct swap_map_handle *handle) static int get_swap_writer(struct swap_map_handle *handle)
...@@ -262,12 +258,7 @@ static int get_swap_writer(struct swap_map_handle *handle) ...@@ -262,12 +258,7 @@ static int get_swap_writer(struct swap_map_handle *handle)
handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL); handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
if (!handle->cur) if (!handle->cur)
return -ENOMEM; return -ENOMEM;
handle->bitmap = alloc_bitmap(count_swap_pages(root_swap, 0)); handle->cur_swap = alloc_swapdev_block(root_swap);
if (!handle->bitmap) {
release_swap_writer(handle);
return -ENOMEM;
}
handle->cur_swap = alloc_swapdev_block(root_swap, handle->bitmap);
if (!handle->cur_swap) { if (!handle->cur_swap) {
release_swap_writer(handle); release_swap_writer(handle);
return -ENOSPC; return -ENOSPC;
...@@ -284,7 +275,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf, ...@@ -284,7 +275,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
if (!handle->cur) if (!handle->cur)
return -EINVAL; return -EINVAL;
offset = alloc_swapdev_block(root_swap, handle->bitmap); offset = alloc_swapdev_block(root_swap);
error = write_page(buf, offset, bio_chain); error = write_page(buf, offset, bio_chain);
if (error) if (error)
return error; return error;
...@@ -293,7 +284,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf, ...@@ -293,7 +284,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
error = wait_on_bio_chain(bio_chain); error = wait_on_bio_chain(bio_chain);
if (error) if (error)
goto out; goto out;
offset = alloc_swapdev_block(root_swap, handle->bitmap); offset = alloc_swapdev_block(root_swap);
if (!offset) if (!offset)
return -ENOSPC; return -ENOSPC;
handle->cur->next_swap = offset; handle->cur->next_swap = offset;
...@@ -430,7 +421,8 @@ int swsusp_write(void) ...@@ -430,7 +421,8 @@ int swsusp_write(void)
} }
} }
if (error) if (error)
free_all_swap_pages(root_swap, handle.bitmap); free_all_swap_pages(root_swap);
release_swap_writer(&handle); release_swap_writer(&handle);
out: out:
swsusp_close(); swsusp_close();
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
#include <linux/syscalls.h> #include <linux/syscalls.h>
#include <linux/highmem.h> #include <linux/highmem.h>
#include <linux/time.h> #include <linux/time.h>
#include <linux/rbtree.h>
#include "power.h" #include "power.h"
...@@ -74,72 +75,69 @@ static inline unsigned int count_highmem_pages(void) { return 0; } ...@@ -74,72 +75,69 @@ static inline unsigned int count_highmem_pages(void) { return 0; }
/** /**
* The following functions are used for tracing the allocated * The following functions are used for tracing the allocated
* swap pages, so that they can be freed in case of an error. * swap pages, so that they can be freed in case of an error.
*
* The functions operate on a linked bitmap structure defined
* in power.h
*/ */
void free_bitmap(struct bitmap_page *bitmap) struct swsusp_extent {
{ struct rb_node node;
struct bitmap_page *bp; unsigned long start;
unsigned long end;
};
while (bitmap) { static struct rb_root swsusp_extents = RB_ROOT;
bp = bitmap->next;
free_page((unsigned long)bitmap);
bitmap = bp;
}
}
struct bitmap_page *alloc_bitmap(unsigned int nr_bits) static int swsusp_extents_insert(unsigned long swap_offset)
{ {
struct bitmap_page *bitmap, *bp; struct rb_node **new = &(swsusp_extents.rb_node);
unsigned int n; struct rb_node *parent = NULL;
struct swsusp_extent *ext;
if (!nr_bits)
return NULL; /* Figure out where to put the new node */
while (*new) {
bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL); ext = container_of(*new, struct swsusp_extent, node);
bp = bitmap; parent = *new;
for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) { if (swap_offset < ext->start) {
bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL); /* Try to merge */
bp = bp->next; if (swap_offset == ext->start - 1) {
if (!bp) { ext->start--;
free_bitmap(bitmap); return 0;
return NULL;
}
} }
return bitmap; new = &((*new)->rb_left);
} } else if (swap_offset > ext->end) {
/* Try to merge */
static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit) if (swap_offset == ext->end + 1) {
{ ext->end++;
unsigned int n; return 0;
n = BITMAP_PAGE_BITS;
while (bitmap && n <= bit) {
n += BITMAP_PAGE_BITS;
bitmap = bitmap->next;
} }
if (!bitmap) new = &((*new)->rb_right);
} else {
/* It already is in the tree */
return -EINVAL; return -EINVAL;
n -= BITMAP_PAGE_BITS;
bit -= n;
n = 0;
while (bit >= BITS_PER_CHUNK) {
bit -= BITS_PER_CHUNK;
n++;
} }
bitmap->chunks[n] |= (1UL << bit); }
/* 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);
return 0; return 0;
} }
sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap) /**
* 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)
{ {
unsigned long offset; unsigned long offset;
offset = swp_offset(get_swap_page_of_type(swap)); offset = swp_offset(get_swap_page_of_type(swap));
if (offset) { if (offset) {
if (bitmap_set(bitmap, offset)) if (swsusp_extents_insert(offset))
swap_free(swp_entry(swap, offset)); swap_free(swp_entry(swap, offset));
else else
return swapdev_block(swap, offset); return swapdev_block(swap, offset);
...@@ -147,23 +145,34 @@ sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap) ...@@ -147,23 +145,34 @@ sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap)
return 0; return 0;
} }
void free_all_swap_pages(int swap, struct bitmap_page *bitmap) /**
* 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)
{ {
unsigned int bit, n; struct rb_node *node;
unsigned long test;
while ((node = swsusp_extents.rb_node)) {
bit = 0; struct swsusp_extent *ext;
while (bitmap) { unsigned long offset;
for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
for (test = 1UL; test; test <<= 1) { ext = container_of(node, struct swsusp_extent, node);
if (bitmap->chunks[n] & test) rb_erase(node, &swsusp_extents);
swap_free(swp_entry(swap, bit)); for (offset = ext->start; offset <= ext->end; offset++)
bit++; swap_free(swp_entry(swap, offset));
}
bitmap = bitmap->next; kfree(ext);
} }
} }
int swsusp_swap_in_use(void)
{
return (swsusp_extents.rb_node != NULL);
}
/** /**
* swsusp_show_speed - print the time elapsed between two events represented by * swsusp_show_speed - print the time elapsed between two events represented by
* @start and @stop * @start and @stop
......
...@@ -33,7 +33,6 @@ ...@@ -33,7 +33,6 @@
static struct snapshot_data { static struct snapshot_data {
struct snapshot_handle handle; struct snapshot_handle handle;
int swap; int swap;
struct bitmap_page *bitmap;
int mode; int mode;
char frozen; char frozen;
char ready; char ready;
...@@ -69,7 +68,6 @@ static int snapshot_open(struct inode *inode, struct file *filp) ...@@ -69,7 +68,6 @@ static int snapshot_open(struct inode *inode, struct file *filp)
data->swap = -1; data->swap = -1;
data->mode = O_WRONLY; data->mode = O_WRONLY;
} }
data->bitmap = NULL;
data->frozen = 0; data->frozen = 0;
data->ready = 0; data->ready = 0;
data->platform_suspend = 0; data->platform_suspend = 0;
...@@ -84,8 +82,7 @@ static int snapshot_release(struct inode *inode, struct file *filp) ...@@ -84,8 +82,7 @@ static int snapshot_release(struct inode *inode, struct file *filp)
swsusp_free(); swsusp_free();
free_basic_memory_bitmaps(); free_basic_memory_bitmaps();
data = filp->private_data; data = filp->private_data;
free_all_swap_pages(data->swap, data->bitmap); free_all_swap_pages(data->swap);
free_bitmap(data->bitmap);
if (data->frozen) { if (data->frozen) {
mutex_lock(&pm_mutex); mutex_lock(&pm_mutex);
thaw_processes(); thaw_processes();
...@@ -300,14 +297,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, ...@@ -300,14 +297,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
error = -ENODEV; error = -ENODEV;
break; break;
} }
if (!data->bitmap) { offset = alloc_swapdev_block(data->swap);
data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0));
if (!data->bitmap) {
error = -ENOMEM;
break;
}
}
offset = alloc_swapdev_block(data->swap, data->bitmap);
if (offset) { if (offset) {
offset <<= PAGE_SHIFT; offset <<= PAGE_SHIFT;
error = put_user(offset, (sector_t __user *)arg); error = put_user(offset, (sector_t __user *)arg);
...@@ -321,13 +311,11 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, ...@@ -321,13 +311,11 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
error = -ENODEV; error = -ENODEV;
break; break;
} }
free_all_swap_pages(data->swap, data->bitmap); free_all_swap_pages(data->swap);
free_bitmap(data->bitmap);
data->bitmap = NULL;
break; break;
case SNAPSHOT_SET_SWAP_FILE: case SNAPSHOT_SET_SWAP_FILE:
if (!data->bitmap) { if (!swsusp_swap_in_use()) {
/* /*
* User space encodes device types as two-byte values, * User space encodes device types as two-byte values,
* so we need to recode them * so we need to recode them
...@@ -426,7 +414,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, ...@@ -426,7 +414,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
break; break;
case SNAPSHOT_SET_SWAP_AREA: case SNAPSHOT_SET_SWAP_AREA:
if (data->bitmap) { if (swsusp_swap_in_use()) {
error = -EPERM; error = -EPERM;
} else { } else {
struct resume_swap_area swap_area; struct resume_swap_area swap_area;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment