Skip to contentSkip to conversation

swsusp: userland interface (rev. 2)

PATCH In review Rafael J. Wysocki 3 participants 19 messages Jan 4, 2006 – Jan 6, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover swsusp: userland interface (rev. 2)

Hi,

This is the second "preview release" of the swsusp userland interface patches. They have changed quite a bit since the previous post, as I tried to make the interface more robust against some potential user space bugs (or outright attempts to abuse it).

The swsusp userland interface is based on a special character device allowing a user space process to initiate suspend or resume using ioctls and to write or read the system memory snapshot from the device (actually more operations are defined on the device).  The device itself is introduced by the second patch.

The first patch adds some low-level snapshot-handling functions (the overall idea is that the in-kernel swap-handling code of swsusp and the userland interface code should use the same set of low-level functions operating on the system memory snapshot). It also modifies the way in which allocated swap pages are handled (traced) by swsusp and, consequently, simplifies the in-kernel swap-writing code (a little).

The last three patches are auxiliary.  The third one moves the in-kernel swap-writing and reading code to a separate file, called swap.c, the fourth one moves the highmem-handling code to swsusp.c, and the last one provides some documentation and makes it possible to compile out the in-kernel swap-writing/reading code (or the userland interface, if desired).

The patches apply on top of three swsusp-related patches that have just made it into the -mm tree, so hopefully they will apply to the next -mm. Anyway, in case you'd like to run some tests, the entire series applicable on top of 2.6.15-rc5-mm3 is available at:
http://www.sisk.pl/kernel/patches/2.6.15-rc5-mm3/
(the other patches in the series are either bugfixes or debug patches, and the order is given by the "series" file).

Also available are very simple demo userland utilities using the interface:
http://www.sisk.pl/kernel/utilities/suspend/
that are also attached to this message.  The "suspend" utility is designed to work from the (root) shell, but the "resume" utility should be invoked from an initrd (I just point /linuxrc to it and start the kernel with the "noresume" command line parameter to prevent the in-kernel resume code from reading the image).  The utilities are intentionally 100% compatible with the in-kernel swap-writing and reading code (ie. the image created by "suspend" can be restored by the in-kernel code etc.).

Any feedback will be very much appreciated.

Greetings,
Rafael

Signature · 1 line
Beer is proof that God loves us and wants us to be happy - Benjamin Franklin

/*
* swsusp.h
*
* Common definitions for user space suspend and resume handlers. *
* Copyright (C) 2005 Rafael J. Wysocki <[email protected]>
*
* This file is released under the GPLv2.
*
*/

#define PAGE_SHIFT 12
#define PAGE_SIZE (1UL << PAGE_SHIFT)

#define SNAPSHOT_IOC_MAGIC '3'
#define SNAPSHOT_IOCFREEZE _IO(SNAPSHOT_IOC_MAGIC, 1)
#define SNAPSHOT_IOCUNFREEZE _IO(SNAPSHOT_IOC_MAGIC, 2)
#define SNAPSHOT_IOCATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
#define SNAPSHOT_IOCATOMIC_RESTORE _IO(SNAPSHOT_IOC_MAGIC, 4)
#define SNAPSHOT_IOCFREE _IO(SNAPSHOT_IOC_MAGIC, 5)
#define SNAPSHOT_IOCSET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
#define SNAPSHOT_IOCAVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
#define SNAPSHOT_IOCGET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
#define SNAPSHOT_IOCFREE_SWAP_PAGES _IO(SNAPSHOT_IOC_MAGIC, 9)
#define SNAPSHOT_IOCSET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
#define SNAPSHOT_IOC_MAXNR 10

#define LINUX_REBOOT_MAGIC1 0xfee1dead
#define LINUX_REBOOT_MAGIC2 672274793

#define LINUX_REBOOT_CMD_RESTART 0x01234567
#define LINUX_REBOOT_CMD_HALT 0xCDEF0123
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321FEDC
#define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4
#define LINUX_REBOOT_CMD_SW_SUSPEND 0xD000FCE2

struct new_utsname {
	char sysname[65];
	char nodename[65];
	char release[65];
	char version[65];
	char machine[65];
	char domainname[65];
};
struct swsusp_info {
	struct new_utsname	uts;
	unsigned int		version_code;
	unsigned long		num_physpages;
	int			cpus;
	unsigned long		image_pages;
	unsigned long		pages;
} __attribute__((aligned(PAGE_SIZE)));

#define SWSUSP_SIG "S1SUSPEND"

struct swsusp_header {
	char reserved[PAGE_SIZE - 20 - sizeof(long)];
	unsigned long image;
	char	orig_sig[10];
	char	sig[10];
} __attribute__((packed, aligned(PAGE_SIZE)));

static inline int freeze(int dev)

{
	return ioctl(dev, SNAPSHOT_IOCFREEZE, 0);
}

static inline int unfreeze(int dev)

{
	return ioctl(dev, SNAPSHOT_IOCUNFREEZE, 0);
}

static inline int atomic_snapshot(int dev, int *in_suspend)

{
	return ioctl(dev, SNAPSHOT_IOCATOMIC_SNAPSHOT, in_suspend);
}

static inline int atomic_restore(int dev)

{
	return ioctl(dev, SNAPSHOT_IOCATOMIC_RESTORE, 0);
}

static inline int free_snapshot(int dev)

{
	return ioctl(dev, SNAPSHOT_IOCFREE, 0);
}

static inline int set_image_size(int dev, unsigned int size)

{
	return ioctl(dev, SNAPSHOT_IOCSET_IMAGE_SIZE, size);
}

static inline void reboot(void)

{
	syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
		LINUX_REBOOT_CMD_RESTART, 0);
}

static inline void power_off(void)

{
	syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
		LINUX_REBOOT_CMD_POWER_OFF, 0);
}

/*
* The swap map is a data structure used for keeping track of each page
* written to a swap partition. It consists of many swap_map_page
* structures that contain each an array of MAP_PAGE_SIZE swap entries.
* These structures are stored on the swap and linked together with the
* help of the .next_swap member.
*
* The swap map is created during suspend. The swap map pages are
* allocated and populated one at a time, so we only need one memory
* page to set up the entire structure.
*
* During resume we also only need to use one swap_map_page structure
* at a time.
*/

#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(long) - 1)

struct swap_map_page {
	unsigned long		entries[MAP_PAGE_ENTRIES];
	unsigned long		next_swap;
};

#define SNAPSHOT_DEVICE "/dev/snapshot"
#define RESUME_DEVICE "/dev/hdc3"

#define DEFAULT_IMAGE_SIZE 500

/*
* suspend.c
*
* A simple user space suspend handler for swsusp. *
* Copyright (C) 2005 Rafael J. Wysocki <[email protected]> *
* This file is released under the GPLv2.
*
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <syscall.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "swsusp.h"

static int dev;
static char buffer[PAGE_SIZE];
static struct swsusp_header swsusp_header;

static inline unsigned int check_free_swap(void)

{
	int error;
	unsigned int free_swap;

	error = ioctl(dev, SNAPSHOT_IOCAVAIL_SWAP, &free_swap);
	if (!error)
		return free_swap;
	else
		printf("Error: errno = %d\n", errno);
	return 0;
}

static inline unsigned long get_swap_page(void)

{
	int error;
	unsigned long offset;

	error = ioctl(dev, SNAPSHOT_IOCGET_SWAP_PAGE, &offset);
	if (!error)
		return offset;
	return 0;
}

static inline int free_swap_pages(void)

{
	return ioctl(dev, SNAPSHOT_IOCFREE_SWAP_PAGES, 0);
}

static inline int set_swap_file(dev_t blkdev)

{
	return ioctl(dev, SNAPSHOT_IOCSET_SWAP_FILE, blkdev);
}

/**
* write_page - Write one page to given swap location.
* @fd: File handle of the resume partition
* @buf: Pointer to the area we're writing.
* @swap_offset: Offset of the swap page we're writing to.
*/

static int write_page(int fd, void *buf, unsigned long swap_offset)

{
	off_t offset;
	int res = -EINVAL;
	ssize_t cnt = 0;

	if (swap_offset) {
		offset = swap_offset * PAGE_SIZE;
		if (lseek(fd, offset, SEEK_SET) == offset) 
			cnt = write(fd, buf, PAGE_SIZE);
		if (cnt == PAGE_SIZE)
			res = 0;
		else
			res = cnt < 0 ? cnt : -EIO;
	}
	return res;
}

/*
* The swap_map_handle structure is used for handling swap in
* a file-alike way
*/

struct swap_map_handle {
	struct swap_map_page cur;
	unsigned long cur_swap;
	unsigned int k;
	int fd;
};

static inline int init_swap_writer(struct swap_map_handle *handle, int fd)

{
	memset(&handle->cur, 0, PAGE_SIZE);
	handle->cur_swap = get_swap_page();
	if (!handle->cur_swap)
		return -ENOSPC;
	handle->k = 0;
	handle->fd = fd;
	return 0;
}

static int swap_write_page(struct swap_map_handle *handle, void *buf)

{
	int error;
	unsigned long offset;

	offset = get_swap_page();
	error = write_page(handle->fd, buf, offset);
	if (error)
		return error;
	handle->cur.entries[handle->k++] = offset;
	if (handle->k >= MAP_PAGE_ENTRIES) {
		offset = get_swap_page();
		if (!offset)
			return -ENOSPC;
		handle->cur.next_swap = offset;
		error = write_page(handle->fd, &handle->cur, handle->cur_swap);
		if (error)
			return error;
		memset(&handle->cur, 0, PAGE_SIZE);
		handle->cur_swap = offset;
		handle->k = 0;
	}
	return 0;
}

static inline int flush_swap_writer(struct swap_map_handle *handle)

{
	if (handle->cur_swap)
		return write_page(handle->fd, &handle->cur, handle->cur_swap);
	else
		return -EINVAL;
}

/**
* save_image - save the suspend image data
*/

static int save_image(struct swap_map_handle *handle,
unsigned int nr_pages)

{
	unsigned int m;
	int ret;
	int error = 0;

	printf("suspend: Saving image data pages (%u pages) ...     ", nr_pages);
	m = nr_pages / 100;
	if (!m)
		m = 1;
	nr_pages = 0;
	do {
		ret = read(dev, buffer, PAGE_SIZE);
		if (ret > 0) {
			error = swap_write_page(handle, buffer);
			if (error)
				break;
			if (!(nr_pages % m))
				printf("\b\b\b\b%3d%%", nr_pages / m);
			nr_pages++;
		}
	} while (ret > 0);
	if (!error)
		printf(" done\n");
	return error;
}

/**
* enough_swap - Make sure we have enough swap to save the image.
*
* Returns TRUE or FALSE after checking the total amount of swap
* space avaiable from the resume partition.
*/

static int enough_swap(unsigned int nr_pages)

{
	unsigned int free_swap = check_free_swap();

	printf("suspend: Free swap pages: %u\n", free_swap);
	return free_swap > nr_pages;
}

static int mark_swap(int fd, unsigned long start)

{
	int error = 0;

	lseek(fd, 0, SEEK_SET);
	if (read(fd, &swsusp_header, PAGE_SIZE) < PAGE_SIZE)
		return -EIO;
	if (!memcmp("SWAP-SPACE", swsusp_header.sig, 10) ||
	    !memcmp("SWAPSPACE2", swsusp_header.sig, 10)) {
		memcpy(swsusp_header.orig_sig, swsusp_header.sig, 10);
		memcpy(swsusp_header.sig, SWSUSP_SIG, 10);
		swsusp_header.image = start;
		lseek(fd, 0, SEEK_SET);
		if (write(fd, &swsusp_header, PAGE_SIZE) < PAGE_SIZE)
			error = -EIO;
	} else {
		printf("suspend: Device is not a swap space.\n");
		error = -ENODEV;
	}
	return error;
}

/**
* write_image - Write entire image and metadata.
*/

int write_image(char *resume_dev_name)

{
	static struct swap_map_handle handle;
	struct swsusp_info *header;
	unsigned long start;
	int fd;
	int error;

	fd = open(resume_dev_name, O_RDWR | O_SYNC);
	if (fd < 0) {
		printf("suspend: Could not open resume device\n");
		return error;
	}
	error = read(dev, buffer, PAGE_SIZE);
	if (error < PAGE_SIZE)
		return error < 0 ? error : -EFAULT;
	header = (struct swsusp_info *)buffer;
	if (!enough_swap(header->pages)) {
		printf("suspend: Not enough free swap\n");
		return -ENOSPC;
	}
	error = init_swap_writer(&handle, fd);
	if (!error) {
		start = handle.cur_swap;
		error = swap_write_page(&handle, header);
	}
	if (!error)
		error = save_image(&handle, header->pages - 1);
	if (!error) {
		flush_swap_writer(&handle);
		printf( "S" );
		error = mark_swap(fd, start);
		printf( "|\n" );
	}
	fsync(fd);
	close(fd);
	return error;
}

int main(int argc, char *argv[])

{
	char *snapshot_device_name, *resume_device_name;
	struct stat *stat_buf;
	int image_size;
	int error;
	int in_suspend;

	resume_device_name = argc <= 2 ? RESUME_DEVICE : argv[2];
	snapshot_device_name = argc <= 1 ? SNAPSHOT_DEVICE : argv[1];

	stat_buf = (struct stat *)buffer;
	if (stat(resume_device_name, stat_buf)) {
		fprintf(stderr, "Could not stat the resume device file\n");
		return -ENODEV;
	}
	if (!S_ISBLK(stat_buf->st_mode)) {
		fprintf(stderr, "Invalid resume device\n");
		return -EINVAL;
	}

	sync();
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
		fprintf(stderr, "Could not lock myself\n");
		return 1;
	}

	error = 0;
	dev = open(snapshot_device_name, O_RDONLY);
	if (dev < 0)
		return -ENOENT;
	if (set_swap_file(stat_buf->st_rdev)) {
		fprintf(stderr, "Could not set the resume device file\n");
		error = errno;
		goto Close;
	}
	image_size = check_free_swap();
	if (image_size <= 0) {
		fprintf(stderr, "No swap space for suspend\n");
		error = -ENOSPC;
		goto Close;
	}
	if (image_size > DEFAULT_IMAGE_SIZE)
		image_size = DEFAULT_IMAGE_SIZE;
	if (freeze(dev)) {
		fprintf(stderr, "Could not freeze processes\n");
		error = errno;
		goto Close;
	}
	while (image_size >= 0) {
		if (set_image_size(dev, image_size)) {
			fprintf(stderr, "Could not set the image size\n");
			error = errno;
			goto Unfreeze;
		}
		if (!atomic_snapshot(dev, &in_suspend)) {
			if (!in_suspend)
				break;
			if (!write_image(resume_device_name)) {
				power_off();
			} else {
				free_swap_pages();
				free_snapshot(dev);
				image_size -= DEFAULT_IMAGE_SIZE;
			}
		}
	}
Unfreeze:
	unfreeze(dev);
Close:
	close(dev);

	return error;
}

/*
* resume.c
*
* A simple user space resume handler for swsusp. *
* Copyright (C) 2005 Rafael J. Wysocki <[email protected]> *
* This file is released under the GPLv2.
*
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <syscall.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "swsusp.h"

static char buffer[PAGE_SIZE];
static struct swsusp_header swsusp_header;

/**
* read_page - Read one page from a swap location.
* @fd: File handle of the resume partition
* @buf: Pointer to the area we're reading into.
* @off: Swap offset of the place to read from.
*/

static int read_page(int fd, void *buf, unsigned long off)

{
	off_t offset;
	int res = 0;
	ssize_t cnt = 0;

	if (off) {
		offset = off * PAGE_SIZE;
		if (lseek(fd, offset, SEEK_SET) == offset) 
			cnt = read(fd, buf, PAGE_SIZE);
		if (cnt < PAGE_SIZE) {
			if (cnt < 0)
				res = cnt;
			else
				res = -EIO;
		}
	}
	return res;
}

/*
* The swap_map_handle structure is used for handling the swap map in
* a file-alike way
*/

struct swap_map_handle {
	struct swap_map_page cur;
	unsigned int k;
	int fd;
};

/**
* The following functions allow us to read data using a swap map
* in a file-alike way
*/

static inline int init_swap_reader(struct swap_map_handle *handle,
int fd, unsigned long start)

{
	int error;

	if (!start)
		return -EINVAL;
	memset(&handle->cur, 0, PAGE_SIZE);
	error = read_page(fd, &handle->cur, start);
	if (error)
		return error;
	handle->fd = fd;
	handle->k = 0;
	return 0;
}

static inline int swap_read_page(struct swap_map_handle *handle, void *buf)

{
	unsigned long offset;
	int error;

	offset = handle->cur.entries[handle->k++];
	if (!offset)
		return -EFAULT;
	error = read_page(handle->fd, buf, offset);
	if (error)
		return error;
	if (handle->k >= MAP_PAGE_ENTRIES) {
		handle->k = 0;
		offset = handle->cur.next_swap;
		if (offset)
			error = read_page(handle->fd, &handle->cur, offset);
		else
			error = -EINVAL;
	}
	return error;
}

/**
* load_image - load the image using the swap map handle
* @handle and the snapshot handle @snapshot
* (assume there are @nr_pages pages to load)
*/

static inline int load_image(struct swap_map_handle *handle, int dev,
unsigned int nr_pages)

{
	unsigned int m, n;
	int ret;
	int error = 0;

	printf("Loading image data pages (%u pages) ...     ", nr_pages);
	m = nr_pages / 100;
	if (!m)
		m = 1;
	n = 0;
	do {
		error = swap_read_page(handle, buffer);
		if (error)
			break;
		if ((ret = write(dev, buffer, PAGE_SIZE)) < PAGE_SIZE) {
			error = ret < 0 ? ret : -ENOSPC;
			break;
		}
		if (!(n % m))
			printf("\b\b\b\b%3d%%", n / m);
		n++;
	} while (n < nr_pages);
	if (!error)
		printf("\b\b\b\bdone\n");
	return error;
}

static int read_image(int dev, char *resume_dev_name)

{
	static struct swap_map_handle handle;
	int fd, ret, error = 0;
	struct swsusp_info *header;
	unsigned int nr_pages;


	fd = open(resume_dev_name, O_RDWR | O_SYNC);
	if (fd < 0) {
		printf("resume: Could not open resume device\n");
		return -ENOENT;
	}
	memset(&swsusp_header, 0, sizeof(swsusp_header));
	ret = read(fd, &swsusp_header, PAGE_SIZE);
	if (ret == PAGE_SIZE) {
		if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
			memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
			/* Reset swap signature now */
			if (lseek(fd, 0, SEEK_SET) == 0) {
				ret = write(fd, &swsusp_header, PAGE_SIZE);
				if (ret < PAGE_SIZE)
					error = ret < 0 ? ret : -EIO;
			} else {
				error = -EIO;
			}
		} else {
			error = -EINVAL;
		}
	} else {
		error = ret < 0 ? ret : -EIO;
	}
	if (!error) {
		error = init_swap_reader(&handle, fd, swsusp_header.image);
		if (!error) {
			header = (struct swsusp_info *)buffer;
			error = swap_read_page(&handle, header);
		}
		if (!error) {
			nr_pages = header->pages - 1;
			ret = write(dev, buffer, PAGE_SIZE);
			if (ret < PAGE_SIZE)
				error = ret < 0 ? ret : -EIO;
		}
		if (!error)
			error = load_image(&handle, dev, nr_pages);
	}
	fsync(fd);
	close(fd);
	if (!error)
		printf("resume: Reading resume file was successful\n");
	else
		printf("resume: Error %d resuming\n", error);
	return error;
}

int main(int argc, char *argv[])

{
	char *snapshot_device_name, *resume_device_name;
	int dev;
	int error = 0;

	resume_device_name = argc <= 2 ? RESUME_DEVICE : argv[2];
	snapshot_device_name = argc <= 1 ? SNAPSHOT_DEVICE : argv[1];

	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
		fprintf(stderr, "resume: Could not lock myself\n");
		return 1;
	}

	dev = open(snapshot_device_name, O_WRONLY);
	if (dev < 0)
		return -ENOENT;
	if (read_image(dev, resume_device_name)) {
		fprintf(stderr, "resume: Could not read the image\n");
		error = errno;
		goto Close;
	}
	if (freeze(dev)) {
		fprintf(stderr, "resume: Could not freeze processes\n");
		error = errno;
		goto Close;
	}
	atomic_restore(dev);
	unfreeze(dev);
Close:
	close(dev);
	return error;
}
#1 lore raw

PATCH 1/5 swsusp: swsusp: low level interface (rev. 2)

This patch introduces the low level interface that can be used for handling the snapshot of the system memory by both the in-kernel swap-writing/reading code of swsusp and the userland interface code introduced by the next patch.

It also changes 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).

 kernel/power/disk.c     |   12 
 kernel/power/power.h    |   25 +
 kernel/power/snapshot.c |  324 +++++++++++++++++++++
 kernel/power/swsusp.c   |  713 +++++++++++++++++++-----------------------------
 4 files changed, 631 insertions(+), 443 deletions(-)
kernel/power/disk.c +4 -8
Index: linux-2.6.15-rc5-mm3/kernel/power/disk.c
===================================================================
@@ -26,9 +26,9 @@
 
 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 @@
 	while(1);
 }
 
-
-static int in_suspend __nosavedata = 0;
-
-
 static inline void platform_finish(void)
 {
 	if (pm_disk_mode == PM_DISK_PLATFORM) {
@@ -152,7 +148,7 @@
 	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 {
@@ -223,7 +219,7 @@
 
 	pr_debug("PM: Reading swsusp image.\n");
 
-	if ((error = swsusp_read(&pagedir_nosave))) {
+	if ((error = swsusp_read())) {
 		swsusp_free();
 		goto Thaw;
 	}
kernel/power/power.h +18 -7
Index: linux-2.6.15-rc5-mm3/kernel/power/power.h
===================================================================
@@ -54,15 +54,26 @@
 /* Preferred image size in MB (default 500) */
 extern unsigned int 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);
kernel/power/swsusp.c +289 -424

957 lines not on this page · raw message

kernel/power/snapshot.c +320 -4

373 lines not on this page · raw message

#2 lore raw

PATCH 2/5 swsusp: userland interface (rev. 2)

In reply to Rafael J. Wysocki

This patch adds a user space interface for swsusp.  The interface is based on the special character device allowing user space processes to perform suspend and resume-related operations with the help of some ioctls and the read()/write() functions.  Additionally it allows these processes to allocate swap pages so that they know which sectors of the resume partition are available to them (it is also possible to free the allocated swap pages).

Currently the major number of the device is allocated dynamically, so it is exported via sysfs for convenience, but I'd like the device to have a well-defined major number in the future, if possible/acceptable.

The interface uses the same low-level snapshot-handling functions that are used by the in-kernel swap-writing/reading code of swsusp.

 init/do_mounts_initrd.c |    1 
 kernel/power/Makefile   |    2 
 kernel/power/power.h    |   46 ++++++
 kernel/power/swsusp.c   |   69 ++++------
 kernel/power/user.c     |  325 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 402 insertions(+), 41 deletions(-)
kernel/power/user.c new file +325 -0
Index: linux-2.6.15-rc5-mm3/kernel/power/user.c
===================================================================
@@ -0,0 +1,325 @@
+/*
+ * linux/kernel/power/user.c
+ *
+ * This file provides the user space interface for software suspend/resume.
+ *
+ * Copyright (C) 2005 Rafael J. Wysocki <[email protected]>
+ *
+ * This file is released under the GPLv2.
+ *
+ */
+
+#include <linux/suspend.h>
+#include <linux/syscalls.h>
+#include <linux/string.h>
+#include <linux/device.h>
+#include <linux/cdev.h>
+#include <linux/mm.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
+#include <linux/pm.h>
+#include <linux/uaccess.h>
+#include <linux/fs.h>
+
+#include "power.h"
+
+struct snapshot_dev {
+	char *name;
+	dev_t devno;
+	struct cdev cdev;
+	struct snapshot_handle handle;
+	int swap;
+	struct bitmap_page *bitmap;
+	int mode;
+	char frozen;
+	char ready;
+};
+
+static atomic_t device_available = ATOMIC_INIT(1);
+
+int snapshot_open(struct inode *inode, struct file *filp)
+{
+	struct snapshot_dev *dev;
+
+	if (!atomic_dec_and_test(&device_available)) {
+		atomic_inc(&device_available);
+		return -EBUSY;
+	}
+
+	if ((filp->f_flags & O_ACCMODE) == O_RDWR)
+		return -ENOSYS;
+
+	nonseekable_open(inode, filp);
+	dev = container_of(inode->i_cdev, struct snapshot_dev, cdev);
+	filp->private_data = dev;
+	memset(&dev->handle, 0, sizeof(struct snapshot_handle));
+	if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
+		dev->swap = swsusp_get_swap_index();
+		dev->mode = O_RDONLY;
+	} else {
+		dev->swap = -1;
+		dev->mode = O_WRONLY;
+	}
+	dev->bitmap = NULL;
+	dev->frozen = 0;
+	dev->ready = 0;
+
+	return 0;
+}
+
+int snapshot_release(struct inode *inode, struct file *filp)
+{
+	struct snapshot_dev *dev;
+
+	swsusp_free();
+	dev = filp->private_data;
+	free_all_swap_pages(dev->swap, dev->bitmap);
+	free_bitmap(dev->bitmap);
+	if (dev->frozen) {
+		down(&pm_sem);
+		thaw_processes();
+		enable_nonboot_cpus();
+		up(&pm_sem);
+	}
+	atomic_inc(&device_available);
+	return 0;
+}
+
+static ssize_t snapshot_read(struct file *filp, char __user *buf,
+                             size_t count, loff_t *offp)
+{
+	struct snapshot_dev *dev;
+	ssize_t res;
+
+	dev = filp->private_data;
+	res = snapshot_read_next(&dev->handle, count);
+	if (res > 0) {
+		if (copy_to_user(buf, data_of(dev->handle), res))
+			res = -EFAULT;
+		else
+			*offp = dev->handle.offset;
+	}
+	return res;
+}
+
+static ssize_t snapshot_write(struct file *filp, const char __user *buf,
+                              size_t count, loff_t *offp)
+{
+	struct snapshot_dev *dev;
+	ssize_t res;
+
+	dev = filp->private_data;
+	res = snapshot_write_next(&dev->handle, count);
+	if (res > 0) {
+		if (copy_from_user(data_of(dev->handle), buf, res))
+			res = -EFAULT;
+		else
+			*offp = dev->handle.offset;
+	}
+	return res;
+}
+
+static int snapshot_ioctl(struct inode *inode, struct file *filp,
+                          unsigned int cmd, unsigned long arg)
+{
+	int error = 0;
+	struct snapshot_dev *dev;
+	unsigned long offset;
+	unsigned int n;
+
+	if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
+		return -ENOTTY;
+	if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
+		return -ENOTTY;
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	dev = filp->private_data;
+
+	switch (cmd) {
+
+	case SNAPSHOT_IOCFREEZE:
+		if (dev->frozen)
+			break;
+		sys_sync();
+		down(&pm_sem);
+		disable_nonboot_cpus();
+		if (freeze_processes())
+			error = -EBUSY;
+		up(&pm_sem);
+		if (!error)
+			dev->frozen = 1;
+		break;
+
+	case SNAPSHOT_IOCUNFREEZE:
+		if (!dev->frozen)
+			break;
+		down(&pm_sem);
+		thaw_processes();
+		enable_nonboot_cpus();
+		up(&pm_sem);
+		dev->frozen = 0;
+		break;
+
+	case SNAPSHOT_IOCATOMIC_SNAPSHOT:
+		if (dev->mode != O_RDONLY || !dev->frozen  || dev->ready) {
+			error = -EPERM;
+			break;
+		}
+		down(&pm_sem);
+		pm_prepare_console();
+		/* Free memory before shutting down devices. */
+		error = swsusp_shrink_memory();
+		if (!error) {
+			error = device_suspend(PMSG_FREEZE);
+			if (!error) {
+				in_suspend = 1;
+				error = swsusp_suspend();
+				device_resume();
+			}
+		}
+		pm_restore_console();
+		up(&pm_sem);
+		if (!error)
+			error = put_user(in_suspend, (unsigned int __user *)arg);
+		if (!error)
+			dev->ready = 1;
+		break;
+
+	case SNAPSHOT_IOCATOMIC_RESTORE:
+		if (dev->mode != O_WRONLY || !dev->frozen ||
+		    !snapshot_image_loaded(&dev->handle)) {
+			error = -EPERM;
+			break;
+		}
+		down(&pm_sem);
+		pm_prepare_console();
+		error = device_suspend(PMSG_FREEZE);
+		if (!error) {
+			mb();
+			error = swsusp_resume();
+			device_resume();
+		}
+		pm_restore_console();
+		up(&pm_sem);
+		break;
+
+	case SNAPSHOT_IOCFREE:
+		swsusp_free();
+		memset(&dev->handle, 0, sizeof(struct snapshot_handle));
+		dev->ready = 0;
+		break;
+
+	case SNAPSHOT_IOCSET_IMAGE_SIZE:
+		image_size = arg;
+		break;
+
+	case SNAPSHOT_IOCAVAIL_SWAP:
+		n = swsusp_available_swap(dev->swap);
+		error = put_user(n, (unsigned int __user *)arg);
+		break;
+
+	case SNAPSHOT_IOCGET_SWAP_PAGE:
+		if (!access_ok(VERIFY_WRITE, (unsigned long __user *)arg, _IOC_SIZE(cmd))) {
+			error = -EINVAL;
+			break;
+		}
+		if (dev->swap < 0 || dev->swap >= MAX_SWAPFILES) {
+			error = -ENODEV;
+			break;
+		}
+		if (!dev->bitmap) {
+			dev->bitmap = alloc_bitmap(swsusp_total_swap(dev->swap));
+			if (!dev->bitmap) {
+				error = -ENOMEM;
+				break;
+			}
+		}
+		offset = alloc_swap_page(dev->swap, dev->bitmap);
+		if (offset)
+			__put_user(offset, (unsigned long __user *)arg);
+		else
+			error = -ENOSPC;
+		break;
+
+	case SNAPSHOT_IOCFREE_SWAP_PAGES:
+		if (dev->swap >= 0 && dev->swap < MAX_SWAPFILES) {
+			error = -ENODEV;
+			break;
+		}
+		free_all_swap_pages(dev->swap, dev->bitmap);
+		free_bitmap(dev->bitmap);
+		dev->bitmap = NULL;
+		break;
+
+	case SNAPSHOT_IOCSET_SWAP_FILE:
+		if (!dev->bitmap) {
+			/*
+			 * User space encodes device types as two-byte values,
+			 * so we need to recode them
+			 */
+			dev->swap = swsusp_get_swap_index_of(old_decode_dev(arg));
+			if (dev->swap < 0)
+				error = -ENODEV;
+		} else {
+			error = -EPERM;
+		}
+		break;
+
+	default:
+		error = -ENOTTY;
+
+	}
+
+	return error;
+}
+
+static struct file_operations snapshot_fops = {
+	.open = snapshot_open,
+	.release = snapshot_release,
+	.read = snapshot_read,
+	.write = snapshot_write,
+	.llseek = no_llseek,
+	.ioctl = snapshot_ioctl,
+};
+
+static struct snapshot_dev interface = {
+	.name = "snapshot",
+};
+
+static ssize_t snapshot_show(struct subsystem * subsys, char *buf)
+{
+	return sprintf(buf, "%d:%d\n", MAJOR(interface.devno),
+		       MINOR(interface.devno));
+}
+
+static struct subsys_attribute snapshot_attr = {
+	.attr = {
+		.name = __stringify(snapshot),
+		.mode = S_IRUGO,
+	},
+	.show = snapshot_show,
+};
+
+static int __init snapshot_dev_init(void)
+{
+	int error;
+
+	error =  alloc_chrdev_region(&interface.devno, 0, 1, interface.name);
+	if (error)
+		return error;
+	cdev_init(&interface.cdev, &snapshot_fops);
+	interface.cdev.ops = &snapshot_fops;
+	error = cdev_add(&interface.cdev, interface.devno, 1);
+	if (error)
+		goto Unregister;
+	error = sysfs_create_file(&power_subsys.kset.kobj, &snapshot_attr.attr);
+	if (!error)
+		return 0;
+	cdev_del(&interface.cdev);
+Unregister:
+	unregister_chrdev_region(interface.devno, 1);
+	return error;
+};
+
+late_initcall(snapshot_dev_init);
kernel/power/Makefile +1 -1
Index: linux-2.6.15-rc5-mm3/kernel/power/Makefile
===================================================================
@@ -5,7 +5,7 @@ endif
 
 obj-y				:= main.o process.o console.o
 obj-$(CONFIG_PM_LEGACY)		+= pm.o
-obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o
+obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o user.o
 
 obj-$(CONFIG_SUSPEND_SMP)	+= smp.o
 
init/do_mounts_initrd.c +1 -0
Index: linux-2.6.15-rc5-mm3/init/do_mounts_initrd.c
===================================================================
@@ -56,6 +56,7 @@ static void __init handle_initrd(void)
 	sys_chroot(".");
 	mount_devfs_fs ();
 
+	current->flags |= PF_NOFREEZE;
 	pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
 	if (pid > 0) {
 		while (pid != sys_wait4(-1, NULL, 0, NULL))
kernel/power/power.h +46 -0

49 lines not on this page · raw message

kernel/power/swsusp.c +29 -40

131 lines not on this page · raw message

#3 lore raw
Quoted 21 lines · Rafael J. Wysocki
kernel/power/user.c new file +21 -0
+static struct snapshot_dev interface = {
+	.name = "snapshot",
+};
+
+static ssize_t snapshot_show(struct subsystem * subsys, char *buf)
+{
+	return sprintf(buf, "%d:%d\n", MAJOR(interface.devno),
+		       MINOR(interface.devno));
+}
+
+static struct subsys_attribute snapshot_attr = {
+	.attr = {
+		.name = __stringify(snapshot),
+		.mode = S_IRUGO,
+	},
+	.show = snapshot_show,
+};
+
+static int __init snapshot_dev_init(void)
+{
+	int error;

kernel/power/user.c line 316

Quoted 10 lines
kernel/power/user.c new file +10 -0
+
+	error =  alloc_chrdev_region(&interface.devno, 0, 1, interface.name);
+	if (error)
+		return error;
+	cdev_init(&interface.cdev, &snapshot_fops);
+	interface.cdev.ops = &snapshot_fops;
+	error = cdev_add(&interface.cdev, interface.devno, 1);
+	if (error)
+		goto Unregister;
+	error = sysfs_create_file(&power_subsys.kset.kobj, &snapshot_attr.attr);

Heh, that's a neat hack, register a sysfs file that contains the major:minor (there is a function that will print that the correct way, if you really want to do that), in sysfs. It's better to just register a misc character device with the name "snapshot", and then udev will create your userspace node with the proper major:minor all automatically for you.

Unless you want to turn these into syscalls :)

thanks,

greg k-h

#4 lore raw

Hi!

Quoted 21 lines

kernel/power/user.c line 316

Quoted 13 lines
kernel/power/user.c new file +13 -0
+static int __init snapshot_dev_init(void)
+{
+	int error;
+
+	error =  alloc_chrdev_region(&interface.devno, 0, 1, interface.name);
+	if (error)
+		return error;
+	cdev_init(&interface.cdev, &snapshot_fops);
+	interface.cdev.ops = &snapshot_fops;
+	error = cdev_add(&interface.cdev, interface.devno, 1);
+	if (error)
+		goto Unregister;
+	error = sysfs_create_file(&power_subsys.kset.kobj, &snapshot_attr.attr);

Heh, that's a neat hack, register a sysfs file that contains the major:minor (there is a function that will print that the correct way, if you really want to do that), in sysfs. It's better to just register a misc character device with the name "snapshot", and then udev will create your userspace node with the proper major:minor all automatically for you.

Quoted 1 line

Unless you want to turn these into syscalls :)

Well, I think we simply want to get static major/minor allocated for this device. It really uses read/write, IIRC, so no, I do not think we want to make it a syscall.
Pavel

Signature · 1 line
Thanks, Sharp!
#5 lore raw
Quoted 23 lines · Pavel Machek

Hi!

Quoted 21 lines

kernel/power/user.c line 316

Quoted 13 lines
kernel/power/user.c new file +13 -0
+static int __init snapshot_dev_init(void)
+{
+	int error;
+
+	error =  alloc_chrdev_region(&interface.devno, 0, 1, interface.name);
+	if (error)
+		return error;
+	cdev_init(&interface.cdev, &snapshot_fops);
+	interface.cdev.ops = &snapshot_fops;
+	error = cdev_add(&interface.cdev, interface.devno, 1);
+	if (error)
+		goto Unregister;
+	error = sysfs_create_file(&power_subsys.kset.kobj, &snapshot_attr.attr);

Heh, that's a neat hack, register a sysfs file that contains the major:minor (there is a function that will print that the correct way, if you really want to do that), in sysfs. It's better to just register a misc character device with the name "snapshot", and then udev will create your userspace node with the proper major:minor all automatically for you.

Quoted 5 lines
Quoted 1 line

Unless you want to turn these into syscalls :)

Well, I think we simply want to get static major/minor allocated for this device. It really uses read/write, IIRC, so no, I do not think we want to make it a syscall.

Ok, then I'd recommend using the misc device, dynamic for now, and reserve one when you get a bit closer to merging into mainline.

thanks,

greg k-h

#6 lore raw
Quoted 24 lines · Greg KH
Quoted 23 lines · Pavel Machek

Hi!

Quoted 21 lines

kernel/power/user.c line 316

Quoted 13 lines
kernel/power/user.c new file +13 -0
+static int __init snapshot_dev_init(void)
+{
+	int error;
+
+	error =  alloc_chrdev_region(&interface.devno, 0, 1, interface.name);
+	if (error)
+		return error;
+	cdev_init(&interface.cdev, &snapshot_fops);
+	interface.cdev.ops = &snapshot_fops;
+	error = cdev_add(&interface.cdev, interface.devno, 1);
+	if (error)
+		goto Unregister;
+	error = sysfs_create_file(&power_subsys.kset.kobj, &snapshot_attr.attr);

Heh, that's a neat hack, register a sysfs file that contains the major:minor (there is a function that will print that the correct way, if you really want to do that), in sysfs. It's better to just register a misc character device with the name "snapshot", and then udev will create your userspace node with the proper major:minor all automatically for you.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Unless you want to turn these into syscalls :)

Well, I think we simply want to get static major/minor allocated for this device. It really uses read/write, IIRC, so no, I do not think we want to make it a syscall.

Ok, then I'd recommend using the misc device, dynamic for now, and reserve one when you get a bit closer to merging into mainline.

I'd say that "memory devices", character, major 1 is appropriate for this. 13 seems to be free ("swsusp can be unlucky for your data"? :-).

Pavel

Signature · 1 line
Thanks, Sharp!
#7 lore raw
In reply to Greg KH

Hi!

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Unless you want to turn these into syscalls :)

Well, I think we simply want to get static major/minor allocated for this device. It really uses read/write, IIRC, so no, I do not think we want to make it a syscall.

Ok, then I'd recommend using the misc device, dynamic for now, and reserve one when you get a bit closer to merging into mainline.

Actually, misc major (character, major 10) seems to be okay, too. There's stuff like /dev/fuse there, already. But it feels little crowded there, and /dev/snapshot seems to fit between memory devices nicely...
Pavel

Signature · 1 line
Thanks, Sharp!
#8 lore raw
In reply to Greg KH

Hi,

Quoted 22 lines · Greg KH
Quoted 21 lines · Pavel Machek
Quoted 21 lines

Review

Quoted 13 lines
(quoted diff) +13 -0
+static int __init snapshot_dev_init(void)
+{
+	int error;
+
+	error =  alloc_chrdev_region(&interface.devno, 0, 1, interface.name);
+	if (error)
+		return error;
+	cdev_init(&interface.cdev, &snapshot_fops);
+	interface.cdev.ops = &snapshot_fops;
+	error = cdev_add(&interface.cdev, interface.devno, 1);
+	if (error)
+		goto Unregister;
+	error = sysfs_create_file(&power_subsys.kset.kobj, &snapshot_attr.attr);

Heh, that's a neat hack, register a sysfs file that contains the major:minor (there is a function that will print that the correct way, if you really want to do that), in sysfs. It's better to just register a misc character device with the name "snapshot", and then udev will create your userspace node with the proper major:minor all automatically for you.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Unless you want to turn these into syscalls :)

Well, I think we simply want to get static major/minor allocated for this device. It really uses read/write, IIRC, so no, I do not think we want to make it a syscall.

Ok, then I'd recommend using the misc device, dynamic for now, and reserve one when you get a bit closer to merging into mainline.

Do you mean something like in the appended patch?

Rafael

kernel/power/user.c new file +294 -0
Index: linux-2.6.15-mm1/kernel/power/user.c
===================================================================
@@ -0,0 +1,294 @@
+/*
+ * linux/kernel/power/user.c
+ *
+ * This file provides the user space interface for software suspend/resume.
+ *
+ * Copyright (C) 2005 Rafael J. Wysocki <[email protected]>
+ *
+ * This file is released under the GPLv2.
+ *
+ */
+
+#include <linux/suspend.h>
+#include <linux/syscalls.h>
+#include <linux/string.h>
+#include <linux/device.h>
+#include <linux/miscdevice.h>
+#include <linux/mm.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
+#include <linux/pm.h>
+#include <linux/uaccess.h>
+#include <linux/fs.h>
+
+#include "power.h"
+
+static struct snapshot_data {
+	struct snapshot_handle handle;
+	int swap;
+	struct bitmap_page *bitmap;
+	int mode;
+	char frozen;
+	char ready;
+} snapshot_state;
+
+static atomic_t device_available = ATOMIC_INIT(1);
+
+int snapshot_open(struct inode *inode, struct file *filp)
+{
+	struct snapshot_data *data;
+
+	if (!atomic_dec_and_test(&device_available)) {
+		atomic_inc(&device_available);
+		return -EBUSY;
+	}
+
+	if ((filp->f_flags & O_ACCMODE) == O_RDWR)
+		return -ENOSYS;
+
+	nonseekable_open(inode, filp);
+	data = &snapshot_state;
+	filp->private_data = data;
+	memset(&data->handle, 0, sizeof(struct snapshot_handle));
+	if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
+		data->swap = swsusp_get_swap_index();
+		data->mode = O_RDONLY;
+	} else {
+		data->swap = -1;
+		data->mode = O_WRONLY;
+	}
+	data->bitmap = NULL;
+	data->frozen = 0;
+	data->ready = 0;
+
+	return 0;
+}
+
+int snapshot_release(struct inode *inode, struct file *filp)
+{
+	struct snapshot_data *data;
+
+	swsusp_free();
+	data = filp->private_data;
+	free_all_swap_pages(data->swap, data->bitmap);
+	free_bitmap(data->bitmap);
+	if (data->frozen) {
+		down(&pm_sem);
+		thaw_processes();
+		enable_nonboot_cpus();
+		up(&pm_sem);
+	}
+	atomic_inc(&device_available);
+	return 0;
+}
+
+static ssize_t snapshot_read(struct file *filp, char __user *buf,
+                             size_t count, loff_t *offp)
+{
+	struct snapshot_data *data;
+	ssize_t res;
+
+	data = filp->private_data;
+	res = snapshot_read_next(&data->handle, count);
+	if (res > 0) {
+		if (copy_to_user(buf, data_of(data->handle), res))
+			res = -EFAULT;
+		else
+			*offp = data->handle.offset;
+	}
+	return res;
+}
+
+static ssize_t snapshot_write(struct file *filp, const char __user *buf,
+                              size_t count, loff_t *offp)
+{
+	struct snapshot_data *data;
+	ssize_t res;
+
+	data = filp->private_data;
+	res = snapshot_write_next(&data->handle, count);
+	if (res > 0) {
+		if (copy_from_user(data_of(data->handle), buf, res))
+			res = -EFAULT;
+		else
+			*offp = data->handle.offset;
+	}
+	return res;
+}
+
+static int snapshot_ioctl(struct inode *inode, struct file *filp,
+                          unsigned int cmd, unsigned long arg)
+{
+	int error = 0;
+	struct snapshot_data *data;
+	unsigned long offset;
+	unsigned int n;
+
+	if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
+		return -ENOTTY;
+	if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
+		return -ENOTTY;
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	data = filp->private_data;
+
+	switch (cmd) {
+
+	case SNAPSHOT_IOCFREEZE:
+		if (data->frozen)
+			break;
+		sys_sync();
+		down(&pm_sem);
+		disable_nonboot_cpus();
+		if (freeze_processes())
+			error = -EBUSY;
+		up(&pm_sem);
+		if (!error)
+			data->frozen = 1;
+		break;
+
+	case SNAPSHOT_IOCUNFREEZE:
+		if (!data->frozen)
+			break;
+		down(&pm_sem);
+		thaw_processes();
+		enable_nonboot_cpus();
+		up(&pm_sem);
+		data->frozen = 0;
+		break;
+
+	case SNAPSHOT_IOCATOMIC_SNAPSHOT:
+		if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
+			error = -EPERM;
+			break;
+		}
+		down(&pm_sem);
+		pm_prepare_console();
+		/* Free memory before shutting down devices. */
+		error = swsusp_shrink_memory();
+		if (!error) {
+			error = device_suspend(PMSG_FREEZE);
+			if (!error) {
+				in_suspend = 1;
+				error = swsusp_suspend();
+				device_resume();
+			}
+		}
+		pm_restore_console();
+		up(&pm_sem);
+		if (!error)
+			error = put_user(in_suspend, (unsigned int __user *)arg);
+		if (!error)
+			data->ready = 1;
+		break;
+
+	case SNAPSHOT_IOCATOMIC_RESTORE:
+		if (data->mode != O_WRONLY || !data->frozen ||
+		    !snapshot_image_loaded(&data->handle)) {
+			error = -EPERM;
+			break;
+		}
+		down(&pm_sem);
+		pm_prepare_console();
+		error = device_suspend(PMSG_FREEZE);
+		if (!error) {
+			mb();
+			error = swsusp_resume();
+			device_resume();
+		}
+		pm_restore_console();
+		up(&pm_sem);
+		break;
+
+	case SNAPSHOT_IOCFREE:
+		swsusp_free();
+		memset(&data->handle, 0, sizeof(struct snapshot_handle));
+		data->ready = 0;
+		break;
+
+	case SNAPSHOT_IOCSET_IMAGE_SIZE:
+		image_size = arg;
+		break;
+
+	case SNAPSHOT_IOCAVAIL_SWAP:
+		n = swsusp_available_swap(data->swap);
+		error = put_user(n, (unsigned int __user *)arg);
+		break;
+
+	case SNAPSHOT_IOCGET_SWAP_PAGE:
+		if (!access_ok(VERIFY_WRITE, (unsigned long __user *)arg, _IOC_SIZE(cmd))) {
+			error = -EINVAL;
+			break;
+		}
+		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
+			error = -ENODEV;
+			break;
+		}
+		if (!data->bitmap) {
+			data->bitmap = alloc_bitmap(swsusp_total_swap(data->swap));
+			if (!data->bitmap) {
+				error = -ENOMEM;
+				break;
+			}
+		}
+		offset = alloc_swap_page(data->swap, data->bitmap);
+		if (offset)
+			__put_user(offset, (unsigned long __user *)arg);
+		else
+			error = -ENOSPC;
+		break;
+
+	case SNAPSHOT_IOCFREE_SWAP_PAGES:
+		if (data->swap >= 0 && data->swap < MAX_SWAPFILES) {
+			error = -ENODEV;
+			break;
+		}
+		free_all_swap_pages(data->swap, data->bitmap);
+		free_bitmap(data->bitmap);
+		data->bitmap = NULL;
+		break;
+
+	case SNAPSHOT_IOCSET_SWAP_FILE:
+		if (!data->bitmap) {
+			/*
+			 * User space encodes device types as two-byte values,
+			 * so we need to recode them
+			 */
+			data->swap = swsusp_get_swap_index_of(old_decode_dev(arg));
+			if (data->swap < 0)
+				error = -ENODEV;
+		} else {
+			error = -EPERM;
+		}
+		break;
+
+	default:
+		error = -ENOTTY;
+
+	}
+
+	return error;
+}
+
+static struct file_operations snapshot_fops = {
+	.open = snapshot_open,
+	.release = snapshot_release,
+	.read = snapshot_read,
+	.write = snapshot_write,
+	.llseek = no_llseek,
+	.ioctl = snapshot_ioctl,
+};
+
+static struct miscdevice snapshot_device = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "snapshot",
+	.fops = &snapshot_fops,
+};
+
+static int __init snapshot_device_init(void)
+{
+	return misc_register(&snapshot_device);
+};
+
+device_initcall(snapshot_device_init);
#9 lore raw
Quoted 4 lines · Rafael J. Wysocki
Quoted 2 lines

Ok, then I'd recommend using the misc device, dynamic for now, and reserve one when you get a bit closer to merging into mainline.

Do you mean something like in the appended patch?

Yes, that looks good to me.

thanks,

greg k-h

#10 lore raw

PATCH 3/5 swsusp: separate swap-writing and reading code (rev. 2)

In reply to Rafael J. Wysocki

This patch moves the in-kernel swap-writing/reading code to a separate file.

 kernel/power/Makefile |    2 
 kernel/power/power.h  |    2 
 kernel/power/swap.c   |  556 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/power/swsusp.c |  535 ------------------------------------------------
 4 files changed, 559 insertions(+), 536 deletions(-)
kernel/power/Makefile +1 -1
Index: linux-2.6.15-rc5-mm3/kernel/power/Makefile
===================================================================
@@ -5,7 +5,7 @@ endif
 
 obj-y				:= main.o process.o console.o
 obj-$(CONFIG_PM_LEGACY)		+= pm.o
-obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o user.o
+obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o user.o swap.o
 
 obj-$(CONFIG_SUSPEND_SMP)	+= smp.o
 
kernel/power/swap.c new file +556 -0

556 lines not on this page · raw message

kernel/power/power.h +1 -1
Index: linux-2.6.15-rc5-mm3/kernel/power/power.h
===================================================================
@@ -53,8 +53,8 @@ extern struct pbe *pagedir_nosave;
 
 /* Preferred image size in MB (default 500) */
 extern unsigned int image_size;
-
 extern int in_suspend;
+extern dev_t swsusp_resume_device;
 
 extern asmlinkage int swsusp_arch_suspend(void);
 extern asmlinkage int swsusp_arch_resume(void);
kernel/power/swsusp.c +1 -534

591 lines not on this page · raw message

#11 lore raw

PATCH 4/5 swsusp: move highmem-handling code to swsusp.c (rev. 2)

In reply to Rafael J. Wysocki

This patch moves all of the highmem-related functions from snapshot.c to swsusp.c.

These functions are only called by the code in swsusp.c and are not related to the snapshot-handling code, so they can be moved from snapshot.c, especially that swsusp.c is much smaller after the swap-writing/reading code has been moved to another file.

 kernel/power/snapshot.c |  113 ------------------------------------------------
 kernel/power/swsusp.c   |  113 ++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 110 insertions(+), 116 deletions(-)
kernel/power/snapshot.c +0 -113
Index: linux-2.6.15-rc5-mm3/kernel/power/snapshot.c
===================================================================
@@ -38,119 +38,6 @@ struct pbe *pagedir_nosave;
 unsigned int nr_copy_pages;
 unsigned int nr_meta_pages;
 
-#ifdef CONFIG_HIGHMEM
-unsigned int count_highmem_pages(void)
-{
-	struct zone *zone;
-	unsigned long zone_pfn;
-	unsigned int n = 0;
-
-	for_each_zone (zone)
-		if (is_highmem(zone)) {
-			mark_free_pages(zone);
-			for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
-				struct page *page;
-				unsigned long pfn = zone_pfn + zone->zone_start_pfn;
-				if (!pfn_valid(pfn))
-					continue;
-				page = pfn_to_page(pfn);
-				if (PageReserved(page))
-					continue;
-				if (PageNosaveFree(page))
-					continue;
-				n++;
-			}
-		}
-	return n;
-}
-
-struct highmem_page {
-	char *data;
-	struct page *page;
-	struct highmem_page *next;
-};
-
-static struct highmem_page *highmem_copy;
-
-static int save_highmem_zone(struct zone *zone)
-{
-	unsigned long zone_pfn;
-	mark_free_pages(zone);
-	for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
-		struct page *page;
-		struct highmem_page *save;
-		void *kaddr;
-		unsigned long pfn = zone_pfn + zone->zone_start_pfn;
-
-		if (!(pfn%1000))
-			printk(".");
-		if (!pfn_valid(pfn))
-			continue;
-		page = pfn_to_page(pfn);
-		/*
-		 * This condition results from rvmalloc() sans vmalloc_32()
-		 * and architectural memory reservations. This should be
-		 * corrected eventually when the cases giving rise to this
-		 * are better understood.
-		 */
-		if (PageReserved(page)) {
-			printk("highmem reserved page?!\n");
-			continue;
-		}
-		BUG_ON(PageNosave(page));
-		if (PageNosaveFree(page))
-			continue;
-		save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
-		if (!save)
-			return -ENOMEM;
-		save->next = highmem_copy;
-		save->page = page;
-		save->data = (void *) get_zeroed_page(GFP_ATOMIC);
-		if (!save->data) {
-			kfree(save);
-			return -ENOMEM;
-		}
-		kaddr = kmap_atomic(page, KM_USER0);
-		memcpy(save->data, kaddr, PAGE_SIZE);
-		kunmap_atomic(kaddr, KM_USER0);
-		highmem_copy = save;
-	}
-	return 0;
-}
-
-int save_highmem(void)
-{
-	struct zone *zone;
-	int res = 0;
-
-	pr_debug("swsusp: Saving Highmem\n");
-	for_each_zone (zone) {
-		if (is_highmem(zone))
-			res = save_highmem_zone(zone);
-		if (res)
-			return res;
-	}
-	return 0;
-}
-
-int restore_highmem(void)
-{
-	printk("swsusp: Restoring Highmem\n");
-	while (highmem_copy) {
-		struct highmem_page *save = highmem_copy;
-		void *kaddr;
-		highmem_copy = save->next;
-
-		kaddr = kmap_atomic(save->page, KM_USER0);
-		memcpy(kaddr, save->data, PAGE_SIZE);
-		kunmap_atomic(kaddr, KM_USER0);
-		free_page((long) save->data);
-		kfree(save);
-	}
-	return 0;
-}
-#endif
-
 static int pfn_is_nosave(unsigned long pfn)
 {
 	unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
kernel/power/swsusp.c +110 -3

119 lines not on this page · raw message

#12 lore raw

PATCH 5/5 swsusp: userland interface documentation and config

In reply to Rafael J. Wysocki
 Documentation/power/userland-swsusp.txt |  120 ++++++++++++++++++++++++++++++++
 kernel/power/Kconfig                    |   74 +++++++++++++------
 kernel/power/Makefile                   |    6 +
 kernel/power/disk.c                     |    1 
 kernel/power/power.h                    |    2 
 kernel/power/swsusp.c                   |    2 
 6 files changed, 179 insertions(+), 26 deletions(-)
kernel/power/Kconfig +51 -23
Index: linux-2.6.15-rc5-mm3/kernel/power/Kconfig
===================================================================
@@ -42,27 +42,65 @@ config SOFTWARE_SUSPEND
 	---help---
 	  Enable the possibility of suspending the machine.
 	  It doesn't need APM.
-	  You may suspend your machine by 'swsusp' or 'shutdown -z <time>' 
-	  (patch for sysvinit needed). 
 
-	  It creates an image which is saved in your active swap. Upon next
-	  boot, pass the 'resume=/dev/swappartition' argument to the kernel to
-	  have it detect the saved image, restore memory state from it, and
-	  continue to run as before. If you do not want the previous state to
-	  be reloaded, then use the 'noresume' kernel argument. However, note
-	  that your partitions will be fsck'd and you must re-mkswap your swap
-	  partitions. It does not work with swap files.
+if SOFTWARE_SUSPEND
 
-	  Right now you may boot without resuming and then later resume but
+config USERLAND_SUSPEND
+	bool "User interface for software suspend"
+	depends on SOFTWARE_SUSPEND
+	default y
+	---help---
+	  Enable the interface allowing user space processes to handle the suspend
+	  and resume of the system.
+
+	  If this interface is used for suspend, the kernel creates a snapshot of
+	  the system memory that may be encrypted, compressed, etc. and has to be
+	  written to a storage by a user space utility.  On resume the image
+	  is read, uncompressed, decrypted, etc. by a user space utility and
+	  transferred to the kernel which restores the system memory state from
+	  it.
+
+	  This is the recommended way of handling software suspend and the
+	  future development will be focused on it, so if you enable software
+	  suspend, you should say Y here.
+
+	  Of course special user space utilities are necessary to use it.
+
+	  For more information please read <file:Documentation/power/userland-swsusp.txt>.
+
+config SWAP_SUSPEND
+	bool "Legacy swap suspend"
+	depends on SOFTWARE_SUSPEND
+	default y
+	---help---
+	  Enable the built-in legacy swap suspend.
+
+	  It allows you to suspend the machine by 'echo disk > /sys/power/state'
+	  or 'shutdown -z <time>' (patch for sysvinit needed).
+
+	  It creates an image which is saved in the swap partition specified
+	  with the 'resume=/dev/swappartition' kernel command line argument.
+
+	  On the next boot, the 'resume=/dev/swappartition' command line argument
+	  causes the kernel to look for the saved image, restore memory state from it,
+	  and continue to run as before. If you do not want the previous state to
+	  be reloaded, then use the 'noresume' kernel argument.  However, note
+	  that your partitions will be fsck'd and you must re-mkswap your resume
+	  partition.  It does not work with swap files.
+
+	  In principle you may boot without resuming and then later resume but
 	  in meantime you cannot use those swap partitions/files which were
-	  involved in suspending. Also in this case there is a risk that buffers
+	  involved in suspending.  Also in this case there is a risk that buffers
 	  on disk won't match with saved ones.
 
+	  You may want to say Y here if your distribution contains power management
+	  utilities that assume this feature to be present.
+
 	  For more information take a look at <file:Documentation/power/swsusp.txt>.
 
 config PM_STD_PARTITION
 	string "Default resume partition"
-	depends on SOFTWARE_SUSPEND
+	depends on SWAP_SUSPEND
 	default ""
 	---help---
 	  The default resume partition is the partition that the suspend-
@@ -82,17 +120,7 @@ config PM_STD_PARTITION
 	  suspended image to. It will simply pick the first available swap 
 	  device.
 
-config SWSUSP_ENCRYPT
-	bool "Encrypt suspend image"
-	depends on SOFTWARE_SUSPEND && CRYPTO=y && (CRYPTO_AES=y || CRYPTO_AES_586=y || CRYPTO_AES_X86_64=y)
-	default ""
-	---help---
-	  To prevent data gathering from swap after resume you can encrypt
-	  the suspend image with a temporary key that is deleted on
-	  resume.
-
-	  Note that the temporary key is stored unencrypted on disk while the
-	  system is suspended.
+endif # SOFTWARE_SUSPEND
 
 config SUSPEND_SMP
 	bool
kernel/power/Makefile +5 -1
Index: linux-2.6.15-rc5-mm3/kernel/power/Makefile
===================================================================
@@ -5,7 +5,11 @@ endif
 
 obj-y				:= main.o process.o console.o
 obj-$(CONFIG_PM_LEGACY)		+= pm.o
-obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o user.o swap.o
+obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o snapshot.o
+
+obj-$(CONFIG_USERLAND_SUSPEND)	+= user.o
+
+obj-$(CONFIG_SWAP_SUSPEND)	+= disk.o swap.o
 
 obj-$(CONFIG_SUSPEND_SMP)	+= smp.o
 
kernel/power/disk.c +0 -1
Index: linux-2.6.15-rc5-mm3/kernel/power/disk.c
===================================================================
@@ -35,7 +35,6 @@ extern int swsusp_resume(void);
 
 static int noresume = 0;
 char resume_file[256] = CONFIG_PM_STD_PARTITION;
-dev_t swsusp_resume_device;
 
 /**
  *	power_down - Shut machine down for hibernate.
kernel/power/swsusp.c +2 -0
Index: linux-2.6.15-rc5-mm3/kernel/power/swsusp.c
===================================================================
@@ -62,6 +62,8 @@ unsigned int image_size = 500;
 
 int in_suspend __nosavedata = 0;
 
+dev_t swsusp_resume_device;
+
 #ifdef CONFIG_HIGHMEM
 static unsigned int count_highmem_pages(void)
 {
kernel/power/power.h +1 -1
Index: linux-2.6.15-rc5-mm3/kernel/power/power.h
===================================================================
@@ -20,7 +20,7 @@ struct swsusp_info {
 
 
 
-#ifdef CONFIG_SOFTWARE_SUSPEND
+#ifdef CONFIG_SWAP_SUSPEND
 extern int pm_suspend_disk(void);
 
 #else
Documentation/power/userland-swsusp.txt new file +120 -0

120 lines not on this page · raw message

#13 lore raw
In reply to Rafael J. Wysocki

Hi!

Quoted 10 lines

This is the second "preview release" of the swsusp userland interface patches. They have changed quite a bit since the previous post, as I tried to make the interface more robust against some potential user space bugs (or outright attempts to abuse it).

The swsusp userland interface is based on a special character device allowing a user space process to initiate suspend or resume using ioctls and to write or read the system memory snapshot from the device (actually more operations are defined on the device).  The device itself is introduced by the
second patch.

...

Quoted 1 line

Any feedback will be very much appreciated.

Pretty please, give it a try. If you think about introducing splashscreen/compression/encryption into swsusp, userland parts of these patches should be great place for it.
Pavel

Signature · 1 line
Thanks, Sharp!
#14 lore raw
In reply to Rafael J. Wysocki

Hi!

Quoted 4 lines

This is the second "preview release" of the swsusp userland interface patches. They have changed quite a bit since the previous post, as I tried to make the interface more robust against some potential user space bugs (or outright attempts to abuse it).

Works for me, thanks.

Perhaps it is time to get 1/4 and 3/4 into -mm? You get my signed-off on them...

2/4 needs to allocate official major/minor. 1/13 would be nice :-).

4/4... I'm not sure. It would be nice to make swsusp.c disappear. It is really wrong name. That means we need to only delete from it for a while...
Pavel

Signature · 1 line
Thanks, Sharp!
#15 lore raw

Hi,

Quoted 9 lines · Pavel Machek
Quoted 4 lines

This is the second "preview release" of the swsusp userland interface patches. They have changed quite a bit since the previous post, as I tried to make the interface more robust against some potential user space bugs (or outright attempts to abuse it).

Works for me, thanks.

Perhaps it is time to get 1/4 and 3/4 into -mm? You get my signed-off on them...

OK, I'll prepare them in a while.

Quoted 1 line

2/4 needs to allocate official major/minor. 1/13 would be nice :-).

Well, you said you liked the patch with a misc device (ie. major = 10).

Actually the code is somewhat simpler in that case so I'd prefer it.

Now, if we used a misc device, which minor would be suitable? 231?

Quoted 3 lines

4/4... I'm not sure. It would be nice to make swsusp.c disappear. It is really wrong name. That means we need to only delete from it for a while...

Anyway I think it would be nice to move the code that does not really belong to the snapshot and is used by both the user interface and disk.c/swap.c to a separate file. I have no preference as far as the name of the file is concerned, though.

Greetings,
Rafael

#16 lore raw

Hi!

Quoted 11 lines
Quoted 9 lines
Quoted 4 lines

This is the second "preview release" of the swsusp userland interface patches. They have changed quite a bit since the previous post, as I tried to make the interface more robust against some potential user space bugs (or outright attempts to abuse it).

Works for me, thanks.

Perhaps it is time to get 1/4 and 3/4 into -mm? You get my signed-off on them...

OK, I'll prepare them in a while.

Thanks.

Quoted 7 lines
Quoted 1 line

2/4 needs to allocate official major/minor. 1/13 would be nice :-).

Well, you said you liked the patch with a misc device (ie. major = 10).

Actually the code is somewhat simpler in that case so I'd prefer it.

Now, if we used a misc device, which minor would be suitable? 231?

If code is simpler, lets stick with misc. You have to obtain minor by mailing [email protected], see Doc*/devices.txt.

Quoted 8 lines
Quoted 3 lines

4/4... I'm not sure. It would be nice to make swsusp.c disappear. It is really wrong name. That means we need to only delete from it for a while...

Anyway I think it would be nice to move the code that does not really belong to the snapshot and is used by both the user interface and disk.c/swap.c to a separate file. I have no preference as far as the name of the file is concerned, though.

Ok, lets keep it as it is. We can always rename file in future. [I don't quite understand your reasons for movement, through. Highmem is part of snapshot we need to make; it is saved in a very different way than rest of memory, but that is implementation detail...]

Pavel

Signature · 1 line
Thanks, Sharp!
#17 lore raw

Hi,

Quoted 5 lines · Pavel Machek
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

This is the second "preview release" of the swsusp userland interface patches. They have changed quite a bit since the previous post, as I tried to make the interface more robust against some potential user space bugs (or outright attempts to abuse it).

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Works for me, thanks.

Perhaps it is time to get 1/4 and 3/4 into -mm? You get my signed-off on them...

OK, I'll prepare them in a while.

Thanks.

I had to remake the 3/4 a bit, as it depended on some changes to power.h and swsusp.c done in the 2/4. Nothing particularly invasive, basically some definitions go to power.h and some function headers change, but please have a look if the patch is still OK (appended).

Quoted 10 lines
Quoted 7 lines
Quoted 1 line

2/4 needs to allocate official major/minor. 1/13 would be nice :-).

Well, you said you liked the patch with a misc device (ie. major = 10).

Actually the code is somewhat simpler in that case so I'd prefer it.

Now, if we used a misc device, which minor would be suitable? 231?

If code is simpler, lets stick with misc. You have to obtain minor by mailing [email protected], see Doc*/devices.txt.

OK, I'll try.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

4/4... I'm not sure. It would be nice to make swsusp.c disappear. It is really wrong name. That means we need to only delete from it for a while...

Anyway I think it would be nice to move the code that does not really belong to the snapshot and is used by both the user interface and disk.c/swap.c to a separate file. I have no preference as far as the name of the file is concerned, though.

Quoted 4 lines

Ok, lets keep it as it is. We can always rename file in future. [I don't quite understand your reasons for movement, through. Highmem is part of snapshot we need to make; it is saved in a very different way than rest of memory, but that is implementation detail...]

I'm seeing this a bit differently. In my view highmem is handled very much like devices: save_highmem() turns it "off", restore_highmem() turns it "on" back again, they are even called next to device_power_up/down().

Besides, the highmem-related code is only invoked from swsusp_suspend/resume() and swsusp_shrink_memory(), so in swsusp.c it can be made static or even static inline (eg. save_highmem() is called only once, and so is count_highmem_pages()).

Greetings,
Rafael

 kernel/power/Makefile |    2 
 kernel/power/power.h  |   35 ++
 kernel/power/swap.c   |  556 ++++++++++++++++++++++++++++++++++++++++++++++
 kernel/power/swsusp.c |  596 ++------------------------------------------------
 4 files changed, 617 insertions(+), 572 deletions(-)
kernel/power/Makefile +1 -1
Index: linux-2.6.15-mm1/kernel/power/Makefile
===================================================================
@@ -5,7 +5,7 @@ endif
 
 obj-y				:= main.o process.o console.o
 obj-$(CONFIG_PM_LEGACY)		+= pm.o
-obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o
+obj-$(CONFIG_SOFTWARE_SUSPEND)	+= swsusp.o disk.o snapshot.o swap.o
 
 obj-$(CONFIG_SUSPEND_SMP)	+= smp.o
 
kernel/power/power.h +34 -1
Index: linux-2.6.15-mm1/kernel/power/power.h
===================================================================
@@ -53,8 +53,8 @@ extern struct pbe *pagedir_nosave;
 
 /* Preferred image size in MB (default 500) */
 extern unsigned int image_size;
-
 extern int in_suspend;
+extern dev_t swsusp_resume_device;
 
 extern asmlinkage int swsusp_arch_suspend(void);
 extern asmlinkage int swsusp_arch_resume(void);
@@ -77,3 +77,36 @@ struct snapshot_handle {
 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);
+
+/**
+ *	The bitmap is used for tracing allocated swap pages
+ *
+ *	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 unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap);
+extern void free_all_swap_pages(int swap, struct bitmap_page *bitmap);
+
+extern int swsusp_get_swap_index_of(dev_t device);
+extern int swsusp_get_swap_index(void);
+extern unsigned int swsusp_total_swap(unsigned int swap);
+extern unsigned int swsusp_available_swap(unsigned int swap);
+extern int swsusp_shrink_memory(void);
+extern int swsusp_suspend(void);
+extern int swsusp_resume(void);
kernel/power/swap.c new file +556 -0

556 lines not on this page · raw message

kernel/power/swsusp.c +26 -570

702 lines not on this page · raw message

#18 lore raw

Hi!

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Perhaps it is time to get 1/4 and 3/4 into -mm? You get my signed-off on them...

OK, I'll prepare them in a while.

Thanks.

I had to remake the 3/4 a bit, as it depended on some changes to power.h and swsusp.c done in the 2/4. Nothing particularly invasive, basically some definitions go to power.h and some function headers change, but please have a look if the patch is still OK (appended).

Still ok :-).

Quoted 4 lines
Quoted 2 lines

If code is simpler, lets stick with misc. You have to obtain minor by mailing [email protected], see Doc*/devices.txt.

OK, I'll try.

It should be easy.

Quoted 8 lines
Quoted 4 lines

Ok, lets keep it as it is. We can always rename file in future. [I don't quite understand your reasons for movement, through. Highmem is part of snapshot we need to make; it is saved in a very different way than rest of memory, but that is implementation detail...]

I'm seeing this a bit differently. In my view highmem is handled very much like devices: save_highmem() turns it "off", restore_highmem() turns it "on" back again, they are even called next to device_power_up/down().

Yes, I guess it is possible to view it like that, too.

BTW your "write 500MB to swap" hit mainline few hours ago. Congratulations.

Pavel

Signature · 1 line
Thanks, Sharp!