All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@suse.cz>
To: jirislaby@gmail.com
Cc: pavel@ucw.cz, linux-pm@lists.linux-foundation.org,
	linux-kernel@vger.kernel.org, Jiri Slaby <jslaby@suse.cz>,
	Nigel Cunningham <ncunningham@crca.org.au>,
	"Rafael J. Wysocki" <rjw@sisk.pl>
Subject: [RFC 12/15] PM / Hibernate: split snapshot_read_next
Date: Tue, 23 Mar 2010 17:17:40 +0100	[thread overview]
Message-ID: <1269361063-3341-12-git-send-email-jslaby@suse.cz> (raw)
In-Reply-To: <1269361063-3341-1-git-send-email-jslaby@suse.cz>

When writing the snapshot, do the initialization and header write in
a separate function. This makes the code more readable and lowers
complexity of snapshot_read_next.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Cc: Nigel Cunningham <ncunningham@crca.org.au>
Cc: "Rafael J. Wysocki" <rjw@sisk.pl>
---
 kernel/power/power.h    |    1 +
 kernel/power/snapshot.c |   63 +++++++++++++++++++++++++++++-----------------
 kernel/power/swap.c     |   14 +++-------
 3 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/kernel/power/power.h b/kernel/power/power.h
index 50a888a..638a97c 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -130,6 +130,7 @@ struct sws_module_ops {
 
 extern unsigned int snapshot_additional_pages(struct zone *zone);
 extern unsigned long snapshot_get_image_size(void);
+extern int snapshot_write_init(struct snapshot_handle *handle);
 extern int snapshot_read_next(struct snapshot_handle *handle);
 extern int snapshot_write_next(struct snapshot_handle *handle);
 extern void snapshot_write_finalize(struct snapshot_handle *handle);
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index 7918351..c8864de 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -1597,10 +1597,44 @@ pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
 }
 
 /**
+ *	snapshot_write_init - initialization before writing the snapshot to
+ *	a backing storage
+ *
+ *	This function *must* be called before snapshot_read_next to initialize
+ *	@handle and write a header.
+ *
+ *	@handle: snapshot handle to init
+ */
+int snapshot_write_init(struct snapshot_handle *handle)
+{
+	int ret;
+
+	/* This makes the buffer be freed by swsusp_free() */
+	buffer = get_image_page(GFP_ATOMIC, PG_ANY);
+	if (!buffer)
+		return -ENOMEM;
+	init_header(buffer);
+	ret = sws_rw_buffer_init(1);
+	if (ret)
+		return ret;
+	ret = sws_rw_buffer(1, buffer, sizeof(struct swsusp_info));
+	if (ret)
+		goto finish;
+	sws_rw_buffer_finish(1);
+	memory_bm_position_reset(&orig_bm);
+	memory_bm_position_reset(&copy_bm);
+	handle->buffer = buffer;
+	return 0;
+finish:
+	sws_rw_buffer_finish(1);
+	return ret;
+}
+
+/**
  *	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
+ *	Before calling this function, snapshot_write_init has to be called with
+ *	handle passed as @handle here. The structure gets updated and a pointer
  *	to it should be passed to this function every next time.
  *
  *	On success the function returns a positive number.  Then, the caller
@@ -1612,31 +1646,12 @@ pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  *	structure pointed to by @handle is not updated and should not be used
  *	any more.
  */
-
 int snapshot_read_next(struct snapshot_handle *handle)
 {
-	if (handle->cur > nr_meta_pages + nr_copy_pages)
-		return 0;
-
-	if (!buffer) {
-		/* This makes the buffer be freed by swsusp_free() */
-		buffer = get_image_page(GFP_ATOMIC, PG_ANY);
-		if (!buffer)
-			return -ENOMEM;
-	}
-	if (!handle->cur) {
-		int error;
-
-		error = init_header((struct swsusp_info *)buffer);
-		if (error)
-			return error;
-		handle->buffer = buffer;
-		memory_bm_position_reset(&orig_bm);
-		memory_bm_position_reset(&copy_bm);
-	} else if (handle->cur <= nr_meta_pages) {
+	if (handle->cur < nr_meta_pages) {
 		memset(buffer, 0, PAGE_SIZE);
 		pack_pfns(buffer, &orig_bm);
-	} else {
+	} else if (handle->cur < nr_meta_pages + nr_copy_pages) {
 		struct page *page;
 
 		page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
@@ -1654,6 +1669,8 @@ int snapshot_read_next(struct snapshot_handle *handle)
 		} else {
 			handle->buffer = page_address(page);
 		}
+	} else {
+		return 0;
 	}
 	handle->cur++;
 	return PAGE_SIZE;
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index cc79ed1..1b0ed28 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -434,7 +434,6 @@ static int enough_space(unsigned int nr_pages)
 int swsusp_write(unsigned int flags)
 {
 	struct snapshot_handle snapshot;
-	struct swsusp_info *header;
 	unsigned long pages;
 	int error;
 
@@ -450,17 +449,12 @@ int swsusp_write(unsigned int flags)
 		goto out_finish;
 	}
 	memset(&snapshot, 0, sizeof(struct snapshot_handle));
-	error = snapshot_read_next(&snapshot);
-	if (error < PAGE_SIZE) {
-		if (error >= 0)
-			error = -EFAULT;
-
+	error = snapshot_write_init(&snapshot);
+	if (error) {
+		printk(KERN_ERR "PM: Cannot init writer\n");
 		goto out_finish;
 	}
-	header = (struct swsusp_info *)data_of(snapshot);
-	error = sws_io_ops->write_page(header, NULL);
-	if (!error)
-		error = save_image(&snapshot, pages - 1);
+	error = save_image(&snapshot, pages - 1);
 out_finish:
 	error = sws_io_ops->put_writer(flags, error);
 	return error;
-- 
1.7.0.2



  parent reply	other threads:[~2010-03-23 16:23 UTC|newest]

Thread overview: 158+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-23 16:17 [RFC 01/15] FS: libfs, implement simple_write_to_buffer Jiri Slaby
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 02/15] PM / Hibernate: snapshot cleanup Jiri Slaby
2010-03-24 20:29   ` Pavel Machek
2010-03-24 20:29   ` Pavel Machek
2010-03-24 22:35   ` Rafael J. Wysocki
2010-03-24 22:35   ` Rafael J. Wysocki
2010-03-25  5:29   ` Pavel Machek
2010-03-25  5:29   ` Pavel Machek
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 03/15] PM / Hibernate: separate block_io Jiri Slaby
2010-03-23 16:17 ` Jiri Slaby
2010-03-24 20:30   ` Pavel Machek
2010-03-24 20:30   ` Pavel Machek
2010-03-24 21:22     ` Jiri Slaby
2010-03-24 22:58       ` Nigel Cunningham
2010-03-25  2:35         ` Nigel Cunningham
2010-03-25  2:35         ` [linux-pm] " Nigel Cunningham
2010-03-25 20:12           ` Rafael J. Wysocki
2010-03-25 20:12           ` [linux-pm] " Rafael J. Wysocki
2010-03-25 20:13             ` Nigel Cunningham
2010-03-25 20:13             ` [linux-pm] " Nigel Cunningham
2010-03-25 20:33               ` Rafael J. Wysocki
2010-03-25 20:33               ` [linux-pm] " Rafael J. Wysocki
2010-03-25 20:36                 ` Nigel Cunningham
2010-03-25 20:36                 ` Nigel Cunningham
2010-03-29 13:30             ` Pavel Machek
2010-03-29 13:30             ` [linux-pm] " Pavel Machek
2010-03-25 14:29         ` Pavel Machek
2010-03-25 14:29         ` Pavel Machek
2010-03-24 22:58       ` Nigel Cunningham
2010-03-24 21:22     ` Jiri Slaby
2010-03-23 16:17 ` [RFC 04/15] PM / Hibernate: move the first_sector out of swsusp_write Jiri Slaby
2010-03-24 20:31   ` Pavel Machek
2010-03-24 20:31   ` Pavel Machek
2010-03-25 21:26     ` Rafael J. Wysocki
2010-03-25 21:26     ` Rafael J. Wysocki
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 05/15] PM / Hibernate: group swap ops Jiri Slaby
2010-03-25 21:28   ` Rafael J. Wysocki
2010-03-25 21:28   ` Rafael J. Wysocki
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 06/15] PM / Hibernate: swap, remove swap_map_handle usages Jiri Slaby
2010-03-24 20:33   ` Pavel Machek
2010-03-24 21:29     ` Jiri Slaby
2010-03-24 21:29     ` Jiri Slaby
2010-03-25 21:35       ` Rafael J. Wysocki
2010-03-25 21:35       ` Rafael J. Wysocki
2010-03-24 20:33   ` Pavel Machek
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 07/15] PM / Hibernate: add sws_modules_ops Jiri Slaby
2010-03-24 20:36   ` Pavel Machek
2010-03-24 21:31     ` Jiri Slaby
2010-03-24 21:31     ` Jiri Slaby
2010-03-24 20:36   ` Pavel Machek
2010-03-25 22:02   ` Rafael J. Wysocki
2010-03-25 22:02   ` Rafael J. Wysocki
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 08/15] PM / Hibernate: add user module_ops Jiri Slaby
2010-03-23 16:17   ` Jiri Slaby
2010-03-25 22:07   ` Rafael J. Wysocki
2010-03-26  9:43     ` Jiri Slaby
2010-03-26  9:43     ` Jiri Slaby
2010-03-25 22:07   ` Rafael J. Wysocki
2010-03-23 16:17 ` [RFC 09/15] PM / Hibernate: user, implement user_ops writer Jiri Slaby
2010-03-24 20:42   ` Pavel Machek
2010-03-24 21:40     ` Jiri Slaby
2010-03-24 21:40     ` Jiri Slaby
2010-03-25 21:36       ` Pavel Machek
2010-03-25 21:36       ` Pavel Machek
2010-03-25 22:14       ` Rafael J. Wysocki
2010-03-25 22:14       ` Rafael J. Wysocki
2010-03-26  9:34         ` Jiri Slaby
2010-03-26 22:04           ` Rafael J. Wysocki
2010-03-29 15:33             ` Jiri Slaby
2010-03-29 15:33             ` Jiri Slaby
2010-03-29 22:09               ` Rafael J. Wysocki
2010-03-30  1:14                 ` Nigel Cunningham
2010-03-30 21:03                   ` Rafael J. Wysocki
2010-03-31  2:31                     ` Nigel Cunningham
2010-03-31 14:47                       ` Pavel Machek
2010-03-31 21:01                         ` Nigel Cunningham
2010-03-31 20:25                       ` Rafael J. Wysocki
2010-03-31 21:06                         ` Nigel Cunningham
2010-03-31 21:36                           ` Rafael J. Wysocki
2010-04-04 23:08                             ` Nigel Cunningham
2010-04-04 23:13                               ` Rafael J. Wysocki
2010-03-31 21:54                           ` Nigel Cunningham
2010-03-30  8:59                 ` Jiri Slaby
2010-03-30 20:50                   ` Rafael J. Wysocki
2010-03-31 14:41                     ` Jiri Slaby
2010-03-31 20:29                       ` Rafael J. Wysocki
2010-04-21 21:22                         ` Jiri Slaby
2010-04-22  3:43                           ` Rafael J. Wysocki
2010-03-26 22:04           ` Rafael J. Wysocki
2010-03-27  7:02           ` Pavel Machek
2010-03-27  7:02           ` Pavel Machek
2010-03-26  9:34         ` Jiri Slaby
2010-03-24 20:42   ` Pavel Machek
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 10/15] PM / Hibernate: user, implement user_ops reader Jiri Slaby
2010-03-25  5:30   ` what the patches do " Pavel Machek
2010-03-25  5:42     ` Nigel Cunningham
2010-03-25  6:12       ` Nigel Cunningham
2010-03-25  6:12       ` [linux-pm] " Nigel Cunningham
2010-03-25 20:14       ` Rafael J. Wysocki
2010-03-25 20:14       ` Rafael J. Wysocki
2010-03-25 20:21         ` Nigel Cunningham
2010-03-25 20:21         ` Nigel Cunningham
2010-03-25 20:29           ` Rafael J. Wysocki
2010-03-25 20:29           ` Rafael J. Wysocki
2010-03-25 20:35             ` Nigel Cunningham
2010-03-25 21:13               ` Rafael J. Wysocki
2010-03-25 21:13               ` Rafael J. Wysocki
2010-03-25 20:35             ` Nigel Cunningham
2010-03-25 21:26             ` Pavel Machek
2010-03-25 21:26             ` Pavel Machek
2010-03-25 21:49               ` [linux-pm] " Nigel Cunningham
2010-04-02  6:36                 ` Pavel Machek
2010-04-02  6:36                 ` [linux-pm] " Pavel Machek
2010-04-02 17:03                   ` Rafael J. Wysocki
2010-04-02 17:03                   ` [linux-pm] " Rafael J. Wysocki
2010-03-25 21:49               ` Nigel Cunningham
2010-03-25  5:42     ` Nigel Cunningham
2010-03-25 22:21     ` Rafael J. Wysocki
2010-03-25 22:21     ` Rafael J. Wysocki
2010-03-25  5:30   ` Pavel Machek
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 11/15] PM / Hibernate: add chunk i/o support Jiri Slaby
2010-03-23 16:17 ` Jiri Slaby
2010-03-25 22:38   ` Rafael J. Wysocki
2010-03-25 22:38   ` Rafael J. Wysocki
2010-03-26  9:09     ` Jiri Slaby
2010-03-26  9:09     ` Jiri Slaby
2010-03-26 10:02       ` Nigel Cunningham
2010-03-26 10:02       ` Nigel Cunningham
2010-03-23 16:17 ` [RFC 12/15] PM / Hibernate: split snapshot_read_next Jiri Slaby
2010-03-23 16:17 ` Jiri Slaby [this message]
2010-03-25 22:44   ` Rafael J. Wysocki
2010-03-25 22:44   ` Rafael J. Wysocki
2010-03-23 16:17 ` [RFC 13/15] PM / Hibernate: split snapshot_write_next Jiri Slaby
2010-03-25 22:46   ` Rafael J. Wysocki
2010-03-25 22:46   ` Rafael J. Wysocki
2010-03-23 16:17 ` Jiri Slaby
2010-03-23 16:17 ` [RFC 14/15] PM / Hibernate: dealign swsusp_info Jiri Slaby
2010-03-23 16:17 ` Jiri Slaby
2010-03-25 22:46   ` Rafael J. Wysocki
2010-03-25 22:46   ` Rafael J. Wysocki
2010-03-23 16:17 ` [RFC 15/15] PM / Hibernate: move non-swap code to snapshot.c Jiri Slaby
2010-03-23 16:17 ` Jiri Slaby
2010-03-25 22:50   ` Rafael J. Wysocki
2010-03-25 22:50   ` Rafael J. Wysocki
2010-03-23 21:51 ` [RFC 01/15] FS: libfs, implement simple_write_to_buffer Rafael J. Wysocki
2010-03-23 21:51 ` Rafael J. Wysocki
2010-03-23 22:09 ` [linux-pm] " Nigel Cunningham
2010-03-23 22:09 ` Nigel Cunningham
2010-03-24 22:13 ` Rafael J. Wysocki
2010-03-24 22:13 ` Rafael J. Wysocki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1269361063-3341-12-git-send-email-jslaby@suse.cz \
    --to=jslaby@suse.cz \
    --cc=jirislaby@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    --cc=ncunningham@crca.org.au \
    --cc=pavel@ucw.cz \
    --cc=rjw@sisk.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.