public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Nigel Cunningham <nigel@tuxonice.net>
To: "Rafael J. Wysocki" <rjw@sisk.pl>
Cc: Linux PM <linux-pm@lists.linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>,
	TuxOnIce-devel <tuxonice-devel@tuxonice.net>
Subject: Re: [PATCH 01/23] Hibernation: Split compression support out.
Date: Tue, 28 Sep 2010 06:32:02 +1000	[thread overview]
Message-ID: <4CA0FF42.2020608@tuxonice.net> (raw)
In-Reply-To: <201009272227.42779.rjw@sisk.pl>

Hi.

On 28/09/10 06:27, Rafael J. Wysocki wrote:
> On Monday, September 27, 2010, Nigel Cunningham wrote:
>> Separate compression support out into its own file, removing in
>> the process the duplication of the load_image and save_image
>> functions and some #includes from swap.c that are no longer
>> needed.
>>
>> Signed-off-by: Nigel Cunningham<nigel@tuxonice.net>
>
> If you're at it, I'd like to do something slightly different.  Explained below.
>
>> ---
>>   kernel/power/Makefile   |    2 +-
>>   kernel/power/compress.c |  210 +++++++++++++++++++++++++++++
>>   kernel/power/compress.h |   23 +++
>>   kernel/power/swap.c     |  339 +++++------------------------------------------
>>   kernel/power/swap.h     |   28 ++++
>>   5 files changed, 296 insertions(+), 306 deletions(-)
>>   create mode 100644 kernel/power/compress.c
>>   create mode 100644 kernel/power/compress.h
>>   create mode 100644 kernel/power/swap.h
>>
>> diff --git a/kernel/power/Makefile b/kernel/power/Makefile
>> index f9063c6..2eb134d 100644
>> --- a/kernel/power/Makefile
>> +++ b/kernel/power/Makefile
>> @@ -9,7 +9,7 @@ obj-$(CONFIG_FREEZER)		+= process.o
>>   obj-$(CONFIG_SUSPEND)		+= suspend.o
>>   obj-$(CONFIG_PM_TEST_SUSPEND)	+= suspend_test.o
>>   obj-$(CONFIG_HIBERNATION)	+= hibernate.o snapshot.o swap.o user.o \
>> -				   block_io.o
>> +				   block_io.o compress.o
>>   obj-$(CONFIG_SUSPEND_NVS)	+= nvs.o
>>
>>   obj-$(CONFIG_MAGIC_SYSRQ)	+= poweroff.o
>> diff --git a/kernel/power/compress.c b/kernel/power/compress.c
>> new file mode 100644
>> index 0000000..45725ea
>> --- /dev/null
>> +++ b/kernel/power/compress.c
>> @@ -0,0 +1,210 @@
>> +/*
>> + * linux/kernel/power/compress.c
>> + *
>> + * This file provides functions for (optionally) compressing an
>> + * image as it is being written and decompressing it at resume.
>> + *
>> + * Copyright (C) 2003-2010 Nigel Cunningham<nigel@tuxonice.net>
>> + *
>> + * This file is released under the GPLv2.
>> + *
>> + */
>> +
>> +#include<linux/slab.h>
>> +#include<linux/lzo.h>
>> +#include<linux/vmalloc.h>
>> +
>> +#include "power.h"
>> +#include "swap.h"
>> +
>> +/* We need to remember how much compressed data we need to read. */
>> +#define LZO_HEADER	sizeof(size_t)
>> +
>> +/* Number of pages/bytes we'll compress at one time. */
>> +#define LZO_UNC_PAGES	32
>> +#define LZO_UNC_SIZE	(LZO_UNC_PAGES * PAGE_SIZE)
>> +
>> +/* Number of pages/bytes we need for compressed data (worst case). */
>> +#define LZO_CMP_PAGES	DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
>> +			             LZO_HEADER, PAGE_SIZE)
>> +#define LZO_CMP_SIZE	(LZO_CMP_PAGES * PAGE_SIZE)
>> +
>> +static size_t off, unc_len, cmp_len;
>> +static unsigned char *unc, *cmp, *wrk, *page;
>> +
>> +void compress_image_cleanup(void)
>> +{
>> +	if (cmp) {
>> +		vfree(cmp);
>> +		cmp = NULL;
>> +	}
>> +
>> +	if (unc) {
>> +		vfree(unc);
>> +		unc = NULL;
>> +	}
>> +
>> +	if (wrk) {
>> +		vfree(wrk);
>> +		wrk = NULL;
>> +	}
>> +
>> +	if (page) {
>> +		free_page((unsigned long)page);
>> +		page = NULL;
>> +	}
>> +}
>> +
>> +int compress_image_init(void)
>> +{
>> +	page = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH);
>> +	if (!page) {
>> +		printk(KERN_ERR "PM: Failed to allocate LZO page\n");
>> +		return -ENOMEM;
>> +	}
>> +
>> +	wrk = vmalloc(LZO1X_1_MEM_COMPRESS);
>> +	unc = vmalloc(LZO_UNC_SIZE);
>> +	cmp = vmalloc(LZO_CMP_SIZE);
>> +
>> +	if (!wrk || !unc || !cmp) {
>> +		printk(KERN_ERR "PM: Failed to allocate memory for (de)compression.\n");
>> +		compress_image_cleanup();
>> +		return -ENOMEM;
>> +	}
>> +
>> +	off = 0;
>> +
>> +	return 0;
>> +}
>> +
>> +static int compress_and_write(struct swap_map_handle *handle, struct bio **bio)
>> +{
>> +	int ret;
>> +
>> +	if (!off)
>> +		return 0;
>> +
>> +	unc_len = off;
>> +	ret = lzo1x_1_compress(unc, unc_len, cmp + LZO_HEADER,&cmp_len, wrk);
>> +
>> +	if (ret<  0) {
>> +		printk(KERN_ERR "PM: LZO compression failed\n");
>> +		return -EIO;
>> +	}
>> +
>> +	if (unlikely(!cmp_len ||
>> +	             cmp_len>  lzo1x_worst_compress(unc_len))) {
>> +		printk(KERN_ERR "PM: Invalid LZO compressed length\n");
>> +		return -EIO;
>> +	}
>> +
>> +	*(size_t *)cmp = cmp_len;
>> +
>> +	/*
>> +	 * Given we are writing one page at a time to disk, we copy
>> +	 * that much from the buffer, although the last bit will likely
>> +	 * be smaller than full page. This is OK - we saved the length
>> +	 * of the compressed data, so any garbage at the end will be
>> +	 * discarded when we read it.
>> +	 */
>> +	for (off = 0; off<  LZO_HEADER + cmp_len; off += PAGE_SIZE) {
>> +		memcpy(page, cmp + off, PAGE_SIZE);
>> +
>> +		ret = swap_write_page(handle, page, bio);
>> +		if (ret)
>> +			return ret;
>> +	}
>> +
>> +	off = 0;
>> +	return 0;
>> +}
>> +
>> +int compress_write(struct swap_map_handle *handle, char *buf, struct bio **bio,
>> +		int flags)
>> +{
>> +	int ret = 0;
>> +
>> +	if (flags&  SF_NOCOMPRESS_MODE)
>> +		return swap_write_page(handle, buf, bio);
>> +
>> +	if (off == LZO_UNC_SIZE)
>> +		ret = compress_and_write(handle, bio);
>> +
>> +	memcpy(unc + off, buf, PAGE_SIZE);
>> +	off += PAGE_SIZE;
>> +	return ret;
>> +}
>
> This one doesn't really good to me.  What I'd prefer would be to have a
> structure of "swap operations" pointers like ->start(), ->write_data(),
> ->read_data(), and ->finish() that will point to the functions in this file
> (if compression is to be used) or to the "old" swap_write_page()/swap_read_page()
> otherwise.  That would reduce the number of the
> (flags&  SF_NOCOMPRESS_MODE) checks quite substantially and will likely result
> in code that's easier to follow.

Me too. I was heading in that direction, but not doing it in one step. 
I'll happily change that.

Nigel

  reply	other threads:[~2010-09-27 20:32 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-27  5:43 Swsusp patches applied to suspend-2.6#linux-next Nigel Cunningham
2010-09-27  5:43 ` [PATCH 01/23] Hibernation: Split compression support out Nigel Cunningham
2010-09-27 20:27   ` Rafael J. Wysocki
2010-09-27 20:32     ` Nigel Cunningham [this message]
2010-10-01 21:28       ` Rafael J. Wysocki
2010-10-01 21:45         ` Nigel Cunningham
2010-10-01 22:15           ` Rafael J. Wysocki
2010-09-27  5:43 ` [PATCH 02/23] Record & display i/o speed post resume Nigel Cunningham
2010-09-27 20:06   ` Rafael J. Wysocki
2010-09-27 20:26     ` Nigel Cunningham
2010-09-27 20:49       ` Rafael J. Wysocki
2010-09-27 21:05         ` Nigel Cunningham
2010-09-27 21:16           ` Rafael J. Wysocki
2010-09-27  5:43 ` [PATCH 03/23] Hibernation: Swap iteration functions Nigel Cunningham
2010-10-04 17:54   ` [linux-pm] " Pavel Machek
2010-10-06  1:22     ` Nigel Cunningham
2010-09-27  5:43 ` [PATCH 04/23] Hibernation: Move root_swap declaration Nigel Cunningham
2010-09-27  5:43 ` [PATCH 05/23] Hibernation: Add mass swap allocation routine Nigel Cunningham
2010-09-27  5:43 ` [PATCH 06/23] Hibernation: Switch to preallocating swap Nigel Cunningham
2010-09-27  5:43 ` [PATCH 07/23] Hiberation: Fix speed display Nigel Cunningham
2010-10-04 17:51   ` [linux-pm] " Pavel Machek
2010-09-27  5:43 ` [PATCH 08/23] Hibernation: Generic extents support Nigel Cunningham
2010-10-04 17:51   ` [linux-pm] " Pavel Machek
2010-10-06  1:21     ` Nigel Cunningham
2010-09-27  5:43 ` [PATCH 09/23] Hibernation: Iterate over sectors not swap entries Nigel Cunningham
2010-09-27  5:43 ` [PATCH 10/23] Hibernation: Stop passing swap_map_handle struct Nigel Cunningham
2010-09-27  5:43 ` [PATCH 11/23] Hibernation: Stop passing bio_chain around Nigel Cunningham
2010-09-27  5:43 ` [PATCH 12/23] Hibernation: Move block i/o fns to block_io.c Nigel Cunningham
2010-09-27  5:43 ` [PATCH 13/23] Hibernation: Partial page I/O support Nigel Cunningham
2010-10-13  7:10   ` Pavel Machek
2010-10-13 20:28     ` Rafael J. Wysocki
2010-10-13 21:17       ` Nigel Cunningham
2010-09-27  5:43 ` [PATCH 14/23] Hibernation: Store block extents at start of image Nigel Cunningham
2010-09-27  5:43 ` [PATCH 15/23] Hibernation: Use block extents for reading image Nigel Cunningham
2010-09-27  5:43 ` [PATCH 16/23] Remove first_sector from swap_map_handle Nigel Cunningham
2010-09-27  5:43 ` [PATCH 17/23] Hibernation: Replace bio chain Nigel Cunningham
2010-09-27  5:43 ` [PATCH 18/23] Hibernation: Remove swap_map_pages Nigel Cunningham
2010-09-27  5:43 ` [PATCH 19/23] Hibernation: Remove wait_on_bio_chain result Nigel Cunningham
2010-09-27  5:43 ` [PATCH 20/23] Hibernation: Prepare for handle.cur removal Nigel Cunningham
2010-09-27  5:43 ` [PATCH 21/23] Hibernation: Remove swap_map structure Nigel Cunningham
2010-09-27  5:43 ` [PATCH 22/23] Hibernation: Remove now-empty routines Nigel Cunningham
2010-09-27  5:43 ` [PATCH 23/23] Hibernate: Implement readahead when resuming Nigel Cunningham
2010-09-27  6:59 ` [TuxOnIce-devel] Swsusp patches applied to suspend-2.6#linux-next Andrey Rahmatullin
2010-09-27  8:28   ` Nigel Cunningham
2010-09-27  8:00 ` Andrey Rahmatullin
2010-09-27  8:29   ` Nigel Cunningham
2010-09-27  9:38     ` Andrey Rahmatullin
2010-09-27  9:57       ` Nigel Cunningham
2010-09-27 11:16     ` Andrey Rahmatullin
2010-09-27 11:39       ` Nigel Cunningham
2010-10-02 16:49 ` [linux-pm] " Martin Steigerwald
2010-10-04  8:00   ` Martin Steigerwald
2010-10-04  8:31     ` Nigel Cunningham
2010-10-04  8:55       ` Martin Steigerwald
2010-10-04  8:47     ` Martin Steigerwald

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=4CA0FF42.2020608@tuxonice.net \
    --to=nigel@tuxonice.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    --cc=rjw@sisk.pl \
    --cc=tuxonice-devel@tuxonice.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox