summaryrefslogtreecommitdiff
path: root/kernel/power/tuxonice_compress.c
blob: d118568b76ff92a17b290154f692a33f3a2f1fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * kernel/power/compression.c
 *
 * Copyright (C) 2003-2015 Nigel Cunningham (nigel at nigelcunningham com au)
 *
 * This file is released under the GPLv2.
 *
 * This file contains data compression routines for TuxOnIce,
 * using cryptoapi.
 */

#include <linux/suspend.h>
#include <linux/highmem.h>
#include <linux/vmalloc.h>
#include <linux/crypto.h>

#include "tuxonice_builtin.h"
#include "tuxonice.h"
#include "tuxonice_modules.h"
#include "tuxonice_sysfs.h"
#include "tuxonice_io.h"
#include "tuxonice_ui.h"
#include "tuxonice_alloc.h"

static int toi_expected_compression;

static struct toi_module_ops toi_compression_ops;
static struct toi_module_ops *next_driver;

static char toi_compressor_name[32] = "lzo";

static DEFINE_MUTEX(stats_lock);

struct cpu_context {
	u8 *page_buffer;
	struct crypto_comp *transform;
	unsigned int len;
	u8 *buffer_start;
	u8 *output_buffer;
};

#define OUT_BUF_SIZE (2 * PAGE_SIZE)

static DEFINE_PER_CPU(struct cpu_context, contexts);

/*
 * toi_crypto_prepare
 *
 * Prepare to do some work by allocating buffers and transforms.
 */
static int toi_compress_crypto_prepare(void)
{
	int cpu;

	if (!*toi_compressor_name) {
		printk(KERN_INFO "TuxOnIce: Compression enabled but no "
				"compressor name set.\n");
		return 1;
	}

	for_each_online_cpu(cpu) {
		struct cpu_context *this = &per_cpu(contexts, cpu);
		this->transform = crypto_alloc_comp(toi_compressor_name, 0, 0);
		if (IS_ERR(this->transform)) {
			printk(KERN_INFO "TuxOnIce: Failed to initialise the "
					"%s compression transform.\n",
					toi_compressor_name);
			this->transform = NULL;
			return 1;
		}

		this->page_buffer =
			(char *) toi_get_zeroed_page(16, TOI_ATOMIC_GFP);

		if (!this->page_buffer) {
			printk(KERN_ERR
			  "Failed to allocate a page buffer for TuxOnIce "
			  "compression driver.\n");
			return -ENOMEM;
		}

		this->output_buffer =
			(char *) vmalloc_32(OUT_BUF_SIZE);

		if (!this->output_buffer) {
			printk(KERN_ERR
			  "Failed to allocate a output buffer for TuxOnIce "
			  "compression driver.\n");
			return -ENOMEM;
		}
	}

	return 0;
}

static int toi_compress_rw_cleanup(int writing)
{
	int cpu;

	for_each_online_cpu(cpu) {
		struct cpu_context *this = &per_cpu(contexts, cpu);
		if (this->transform) {
			crypto_free_comp(this->transform);
			this->transform = NULL;
		}

		if (this->page_buffer)
			toi_free_page(16, (unsigned long) this->page_buffer);

		this->page_buffer = NULL;

		if (this->output_buffer)
			vfree(this->output_buffer);

		this->output_buffer = NULL;
	}

	return 0;
}

/*
 * toi_compress_init
 */

static int toi_compress_init(int toi_or_resume)
{
	if (!toi_or_resume)
		return 0;

	toi_compress_bytes_in = 0;
	toi_compress_bytes_out = 0;

	next_driver = toi_get_next_filter(&toi_compression_ops);

	return next_driver ? 0 : -ECHILD;
}

/*
 * toi_compress_rw_init()
 */

static int toi_compress_rw_init(int rw, int stream_number)
{
	if (toi_compress_crypto_prepare()) {
		printk(KERN_ERR "Failed to initialise compression "
				"algorithm.\n");
		if (rw == READ) {
			printk(KERN_INFO "Unable to read the image.\n");
			return -ENODEV;
		} else {
			printk(KERN_INFO "Continuing without "
				"compressing the image.\n");
			toi_compression_ops.enabled = 0;
		}
	}

	return 0;
}

/*
 * toi_compress_write_page()
 *
 * Compress a page of data, buffering output and passing on filled
 * pages to the next module in the pipeline.
 *
 * Buffer_page:	Pointer to a buffer of size PAGE_SIZE, containing
 * data to be compressed.
 *
 * Returns:	0 on success. Otherwise the error is that returned by later
 * 		modules, -ECHILD if we have a broken pipeline or -EIO if
 * 		zlib errs.
 */
static int toi_compress_write_page(unsigned long index, int buf_type,
		void *buffer_page, unsigned int buf_size)
{
	int ret = 0, cpu = smp_processor_id();
	struct cpu_context *ctx = &per_cpu(contexts, cpu);
	u8* output_buffer = buffer_page;
	int output_len = buf_size;
	int out_buf_type = buf_type;

	if (ctx->transform) {

		ctx->buffer_start = TOI_MAP(buf_type, buffer_page);
		ctx->len = OUT_BUF_SIZE;

		ret = crypto_comp_compress(ctx->transform,
			ctx->buffer_start, buf_size,
			ctx->output_buffer, &ctx->len);

		TOI_UNMAP(buf_type, buffer_page);

		toi_message(TOI_COMPRESS, TOI_VERBOSE, 0,
				"CPU %d, index %lu: %d bytes",
				cpu, index, ctx->len);

		if (!ret && ctx->len < buf_size) { /* some compression */
			output_buffer = ctx->output_buffer;
			output_len = ctx->len;
			out_buf_type = TOI_VIRT;
		}

	}

	mutex_lock(&stats_lock);

	toi_compress_bytes_in += buf_size;
	toi_compress_bytes_out += output_len;

	mutex_unlock(&stats_lock);

	if (!ret)
		ret = next_driver->write_page(index, out_buf_type,
				output_buffer, output_len);

	return ret;
}

/*
 * toi_compress_read_page()
 * @buffer_page: struct page *. Pointer to a buffer of size PAGE_SIZE.
 *
 * Retrieve data from later modules and decompress it until the input buffer
 * is filled.
 * Zero if successful. Error condition from me or from downstream on failure.
 */
static int toi_compress_read_page(unsigned long *index, int buf_type,
		void *buffer_page, unsigned int *buf_size)
{
	int ret, cpu = smp_processor_id();
	unsigned int len;
	unsigned int outlen = PAGE_SIZE;
	char *buffer_start;
	struct cpu_context *ctx = &per_cpu(contexts, cpu);

	if (!ctx->transform)
		return next_driver->read_page(index, TOI_PAGE, buffer_page,
				buf_size);

	/*
	 * All our reads must be synchronous - we can't decompress
	 * data that hasn't been read yet.
	 */

	ret = next_driver->read_page(index, TOI_VIRT, ctx->page_buffer, &len);

	buffer_start = kmap(buffer_page);

	/* Error or uncompressed data */
	if (ret || len == PAGE_SIZE) {
		memcpy(buffer_start, ctx->page_buffer, len);
		goto out;
	}

	ret = crypto_comp_decompress(
			ctx->transform,
			ctx->page_buffer,
			len, buffer_start, &outlen);

	toi_message(TOI_COMPRESS, TOI_VERBOSE, 0,
			"CPU %d, index %lu: %d=>%d (%d).",
			cpu, *index, len, outlen, ret);

	if (ret)
		abort_hibernate(TOI_FAILED_IO,
			"Compress_read returned %d.\n", ret);
	else if (outlen != PAGE_SIZE) {
		abort_hibernate(TOI_FAILED_IO,
			"Decompression yielded %d bytes instead of %ld.\n",
			outlen, PAGE_SIZE);
		printk(KERN_ERR "Decompression yielded %d bytes instead of "
				"%ld.\n", outlen, PAGE_SIZE);
		ret = -EIO;
		*buf_size = outlen;
	}
out:
	TOI_UNMAP(buf_type, buffer_page);
	return ret;
}

/*
 * toi_compress_print_debug_stats
 * @buffer: Pointer to a buffer into which the debug info will be printed.
 * @size: Size of the buffer.
 *
 * Print information to be recorded for debugging purposes into a buffer.
 * Returns: Number of characters written to the buffer.
 */

static int toi_compress_print_debug_stats(char *buffer, int size)
{
	unsigned long pages_in = toi_compress_bytes_in >> PAGE_SHIFT,
		      pages_out = toi_compress_bytes_out >> PAGE_SHIFT;
	int len;

	/* Output the compression ratio achieved. */
	if (*toi_compressor_name)
		len = scnprintf(buffer, size, "- Compressor is '%s'.\n",
				toi_compressor_name);
	else
		len = scnprintf(buffer, size, "- Compressor is not set.\n");

	if (pages_in)
		len += scnprintf(buffer+len, size - len, "  Compressed "
			"%lu bytes into %lu (%ld percent compression).\n",
		  toi_compress_bytes_in,
		  toi_compress_bytes_out,
		  (pages_in - pages_out) * 100 / pages_in);
	return len;
}

/*
 * toi_compress_compression_memory_needed
 *
 * Tell the caller how much memory we need to operate during hibernate/resume.
 * Returns: Unsigned long. Maximum number of bytes of memory required for
 * operation.
 */
static int toi_compress_memory_needed(void)
{
	return 2 * PAGE_SIZE;
}

static int toi_compress_storage_needed(void)
{
	return 2 * sizeof(unsigned long) + 2 * sizeof(int) +
		strlen(toi_compressor_name) + 1;
}

/*
 * toi_compress_save_config_info
 * @buffer: Pointer to a buffer of size PAGE_SIZE.
 *
 * Save informaton needed when reloading the image at resume time.
 * Returns: Number of bytes used for saving our data.
 */
static int toi_compress_save_config_info(char *buffer)
{
	int len = strlen(toi_compressor_name) + 1, offset = 0;

	*((unsigned long *) buffer) = toi_compress_bytes_in;
	offset += sizeof(unsigned long);
	*((unsigned long *) (buffer + offset)) = toi_compress_bytes_out;
	offset += sizeof(unsigned long);
	*((int *) (buffer + offset)) = toi_expected_compression;
	offset += sizeof(int);
	*((int *) (buffer + offset)) = len;
	offset += sizeof(int);
	strncpy(buffer + offset, toi_compressor_name, len);
	return offset + len;
}

/* toi_compress_load_config_info
 * @buffer: Pointer to the start of the data.
 * @size: Number of bytes that were saved.
 *
 * Description:	Reload information needed for decompressing the image at
 * resume time.
 */
static void toi_compress_load_config_info(char *buffer, int size)
{
	int len, offset = 0;

	toi_compress_bytes_in = *((unsigned long *) buffer);
	offset += sizeof(unsigned long);
	toi_compress_bytes_out = *((unsigned long *) (buffer + offset));
	offset += sizeof(unsigned long);
	toi_expected_compression = *((int *) (buffer + offset));
	offset += sizeof(int);
	len = *((int *) (buffer + offset));
	offset += sizeof(int);
	strncpy(toi_compressor_name, buffer + offset, len);
}

static void toi_compress_pre_atomic_restore(struct toi_boot_kernel_data *bkd)
{
	bkd->compress_bytes_in = toi_compress_bytes_in;
	bkd->compress_bytes_out = toi_compress_bytes_out;
}

static void toi_compress_post_atomic_restore(struct toi_boot_kernel_data *bkd)
{
	toi_compress_bytes_in = bkd->compress_bytes_in;
	toi_compress_bytes_out = bkd->compress_bytes_out;
}

/*
 * toi_expected_compression_ratio
 *
 * Description:	Returns the expected ratio between data passed into this module
 * 		and the amount of data output when writing.
 * Returns:	100 if the module is disabled. Otherwise the value set by the
 * 		user via our sysfs entry.
 */

static int toi_compress_expected_ratio(void)
{
	if (!toi_compression_ops.enabled)
		return 100;
	else
		return 100 - toi_expected_compression;
}

/*
 * data for our sysfs entries.
 */
static struct toi_sysfs_data sysfs_params[] = {
	SYSFS_INT("expected_compression", SYSFS_RW, &toi_expected_compression,
			0, 99, 0, NULL),
	SYSFS_INT("enabled", SYSFS_RW, &toi_compression_ops.enabled, 0, 1, 0,
			NULL),
	SYSFS_STRING("algorithm", SYSFS_RW, toi_compressor_name, 31, 0, NULL),
};

/*
 * Ops structure.
 */
static struct toi_module_ops toi_compression_ops = {
	.type			= FILTER_MODULE,
	.name			= "compression",
	.directory		= "compression",
	.module			= THIS_MODULE,
	.initialise		= toi_compress_init,
	.memory_needed 		= toi_compress_memory_needed,
	.print_debug_info	= toi_compress_print_debug_stats,
	.save_config_info	= toi_compress_save_config_info,
	.load_config_info	= toi_compress_load_config_info,
	.storage_needed		= toi_compress_storage_needed,
	.expected_compression	= toi_compress_expected_ratio,

	.pre_atomic_restore	= toi_compress_pre_atomic_restore,
	.post_atomic_restore	= toi_compress_post_atomic_restore,

	.rw_init		= toi_compress_rw_init,
	.rw_cleanup		= toi_compress_rw_cleanup,

	.write_page		= toi_compress_write_page,
	.read_page		= toi_compress_read_page,

	.sysfs_data		= sysfs_params,
	.num_sysfs_entries	= sizeof(sysfs_params) /
		sizeof(struct toi_sysfs_data),
};

/* ---- Registration ---- */

static __init int toi_compress_load(void)
{
	return toi_register_module(&toi_compression_ops);
}

late_initcall(toi_compress_load);