summaryrefslogtreecommitdiff
path: root/kernel/power/tuxonice_checksum.c
blob: 8952c0fec410f1a45687354ab84ba475c77da592 (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
/*
 * kernel/power/tuxonice_checksum.c
 *
 * Copyright (C) 2006-2015 Nigel Cunningham (nigel at nigelcunningham com au)
 *
 * This file is released under the GPLv2.
 *
 * This file contains data checksum routines for TuxOnIce,
 * using cryptoapi. They are used to locate any modifications
 * made to pageset 2 while we're saving it.
 */

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

#include "tuxonice.h"
#include "tuxonice_modules.h"
#include "tuxonice_sysfs.h"
#include "tuxonice_io.h"
#include "tuxonice_pageflags.h"
#include "tuxonice_checksum.h"
#include "tuxonice_pagedir.h"
#include "tuxonice_alloc.h"
#include "tuxonice_ui.h"

static struct toi_module_ops toi_checksum_ops;

/* Constant at the mo, but I might allow tuning later */
static char toi_checksum_name[32] = "md4";
/* Bytes per checksum */
#define CHECKSUM_SIZE (16)

#define CHECKSUMS_PER_PAGE ((PAGE_SIZE - sizeof(void *)) / CHECKSUM_SIZE)

struct cpu_context {
	struct crypto_hash *transform;
	struct hash_desc desc;
	struct scatterlist sg[2];
	char *buf;
};

static DEFINE_PER_CPU(struct cpu_context, contexts);
static int pages_allocated;
static unsigned long page_list;

static int toi_num_resaved;

static unsigned long this_checksum, next_page;
static int checksum_count;

static inline int checksum_pages_needed(void)
{
	return DIV_ROUND_UP(pagedir2.size, CHECKSUMS_PER_PAGE);
}

/* ---- Local buffer management ---- */

/*
 * toi_checksum_cleanup
 *
 * Frees memory allocated for our labours.
 */
static void toi_checksum_cleanup(int ending_cycle)
{
	int cpu;

	if (ending_cycle) {
		for_each_online_cpu(cpu) {
			struct cpu_context *this = &per_cpu(contexts, cpu);
			if (this->transform) {
				crypto_free_hash(this->transform);
				this->transform = NULL;
				this->desc.tfm = NULL;
			}

			if (this->buf) {
				toi_free_page(27, (unsigned long) this->buf);
				this->buf = NULL;
			}
		}
	}
}

/*
 * toi_crypto_initialise
 *
 * Prepare to do some work by allocating buffers and transforms.
 * Returns: Int: Zero. Even if we can't set up checksum, we still
 * seek to hibernate.
 */
static int toi_checksum_initialise(int starting_cycle)
{
	int cpu;

	if (!(starting_cycle & SYSFS_HIBERNATE) || !toi_checksum_ops.enabled)
		return 0;

	if (!*toi_checksum_name) {
		printk(KERN_INFO "TuxOnIce: No checksum algorithm name set.\n");
		return 1;
	}

	for_each_online_cpu(cpu) {
		struct cpu_context *this = &per_cpu(contexts, cpu);
		struct page *page;

		this->transform = crypto_alloc_hash(toi_checksum_name, 0, 0);
		if (IS_ERR(this->transform)) {
			printk(KERN_INFO "TuxOnIce: Failed to initialise the "
				"%s checksum algorithm: %ld.\n",
				toi_checksum_name, (long) this->transform);
			this->transform = NULL;
			return 1;
		}

		this->desc.tfm = this->transform;
		this->desc.flags = 0;

		page = toi_alloc_page(27, GFP_KERNEL);
		if (!page)
			return 1;
		this->buf = page_address(page);
		sg_init_one(&this->sg[0], this->buf, PAGE_SIZE);
	}
	return 0;
}

/*
 * toi_checksum_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_checksum_print_debug_stats(char *buffer, int size)
{
	int len;

	if (!toi_checksum_ops.enabled)
		return scnprintf(buffer, size,
			"- Checksumming disabled.\n");

	len = scnprintf(buffer, size, "- Checksum method is '%s'.\n",
			toi_checksum_name);
	len += scnprintf(buffer + len, size - len,
		"  %d pages resaved in atomic copy.\n", toi_num_resaved);
	return len;
}

static int toi_checksum_memory_needed(void)
{
	return toi_checksum_ops.enabled ?
		checksum_pages_needed() << PAGE_SHIFT : 0;
}

static int toi_checksum_storage_needed(void)
{
	if (toi_checksum_ops.enabled)
		return strlen(toi_checksum_name) + sizeof(int) + 1;
	else
		return 0;
}

/*
 * toi_checksum_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_checksum_save_config_info(char *buffer)
{
	int namelen = strlen(toi_checksum_name) + 1;
	int total_len;

	*((unsigned int *) buffer) = namelen;
	strncpy(buffer + sizeof(unsigned int), toi_checksum_name, namelen);
	total_len = sizeof(unsigned int) + namelen;
	return total_len;
}

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

	namelen = *((unsigned int *) (buffer));
	strncpy(toi_checksum_name, buffer + sizeof(unsigned int),
			namelen);
	return;
}

/*
 * Free Checksum Memory
 */

void free_checksum_pages(void)
{
	while (pages_allocated) {
		unsigned long next = *((unsigned long *) page_list);
		ClearPageNosave(virt_to_page(page_list));
		toi_free_page(15, (unsigned long) page_list);
		page_list = next;
		pages_allocated--;
	}
}

/*
 * Allocate Checksum Memory
 */

int allocate_checksum_pages(void)
{
	int pages_needed = checksum_pages_needed();

	if (!toi_checksum_ops.enabled)
		return 0;

	while (pages_allocated < pages_needed) {
		unsigned long *new_page =
		  (unsigned long *) toi_get_zeroed_page(15, TOI_ATOMIC_GFP);
		if (!new_page) {
			printk(KERN_ERR "Unable to allocate checksum pages.\n");
			return -ENOMEM;
		}
		SetPageNosave(virt_to_page(new_page));
		(*new_page) = page_list;
		page_list = (unsigned long) new_page;
		pages_allocated++;
	}

	next_page = (unsigned long) page_list;
	checksum_count = 0;

	return 0;
}

char *tuxonice_get_next_checksum(void)
{
	if (!toi_checksum_ops.enabled)
		return NULL;

	if (checksum_count % CHECKSUMS_PER_PAGE)
		this_checksum += CHECKSUM_SIZE;
	else {
		this_checksum = next_page + sizeof(void *);
		next_page = *((unsigned long *) next_page);
	}

	checksum_count++;
	return (char *) this_checksum;
}

int tuxonice_calc_checksum(struct page *page, char *checksum_locn)
{
	char *pa;
	int result, cpu = smp_processor_id();
	struct cpu_context *ctx = &per_cpu(contexts, cpu);

	if (!toi_checksum_ops.enabled)
		return 0;

	pa = kmap(page);
	memcpy(ctx->buf, pa, PAGE_SIZE);
	kunmap(page);
	result = crypto_hash_digest(&ctx->desc, ctx->sg, PAGE_SIZE,
						checksum_locn);
	if (result)
		printk(KERN_ERR "TuxOnIce checksumming: crypto_hash_digest "
				"returned %d.\n", result);
	return result;
}
/*
 * Calculate checksums
 */

void check_checksums(void)
{
	int index = 0, cpu = smp_processor_id();
	char current_checksum[CHECKSUM_SIZE];
	struct cpu_context *ctx = &per_cpu(contexts, cpu);
        unsigned long pfn;

	if (!toi_checksum_ops.enabled) {
		toi_message(TOI_IO, TOI_VERBOSE, 0, "Checksumming disabled.");
		return;
	}

	next_page = (unsigned long) page_list;

	toi_num_resaved = 0;
	this_checksum = 0;

        toi_trace_index++;

	toi_message(TOI_IO, TOI_VERBOSE, 0, "Verifying checksums.");
	memory_bm_position_reset(pageset2_map);
	for (pfn = memory_bm_next_pfn(pageset2_map, 0); pfn != BM_END_OF_MAP;
			pfn = memory_bm_next_pfn(pageset2_map, 0)) {
		int ret, resave_needed = false;
		char *pa;
		struct page *page = pfn_to_page(pfn);

                if (index < checksum_count) {
                    if (index % CHECKSUMS_PER_PAGE) {
                        this_checksum += CHECKSUM_SIZE;
                    } else {
                        this_checksum = next_page + sizeof(void *);
                        next_page = *((unsigned long *) next_page);
                    }

                    /* Done when IRQs disabled so must be atomic */
                    pa = kmap_atomic(page);
                    memcpy(ctx->buf, pa, PAGE_SIZE);
                    kunmap_atomic(pa);
                    ret = crypto_hash_digest(&ctx->desc, ctx->sg, PAGE_SIZE,
                            current_checksum);

                    if (ret) {
                        printk(KERN_INFO "Digest failed. Returned %d.\n", ret);
                        return;
                    }

                    resave_needed = memcmp(current_checksum, (char *) this_checksum,
                            CHECKSUM_SIZE);
                } else {
                    resave_needed = true;
                }

                if (resave_needed) {
			TOI_TRACE_DEBUG(pfn, "_Resaving %d", resave_needed);
			SetPageResave(pfn_to_page(pfn));
			toi_num_resaved++;
			if (test_action_state(TOI_ABORT_ON_RESAVE_NEEDED))
				set_abort_result(TOI_RESAVE_NEEDED);
		}

		index++;
	}
	toi_message(TOI_IO, TOI_VERBOSE, 0, "Checksum verification complete.");
}

static struct toi_sysfs_data sysfs_params[] = {
	SYSFS_INT("enabled", SYSFS_RW, &toi_checksum_ops.enabled, 0, 1, 0,
			NULL),
	SYSFS_BIT("abort_if_resave_needed", SYSFS_RW, &toi_bkd.toi_action,
			TOI_ABORT_ON_RESAVE_NEEDED, 0)
};

/*
 * Ops structure.
 */
static struct toi_module_ops toi_checksum_ops = {
	.type			= MISC_MODULE,
	.name			= "checksumming",
	.directory		= "checksum",
	.module			= THIS_MODULE,
	.initialise		= toi_checksum_initialise,
	.cleanup		= toi_checksum_cleanup,
	.print_debug_info	= toi_checksum_print_debug_stats,
	.save_config_info	= toi_checksum_save_config_info,
	.load_config_info	= toi_checksum_load_config_info,
	.memory_needed		= toi_checksum_memory_needed,
	.storage_needed		= toi_checksum_storage_needed,

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

/* ---- Registration ---- */
int toi_checksum_init(void)
{
	int result = toi_register_module(&toi_checksum_ops);
	return result;
}

void toi_checksum_exit(void)
{
	toi_unregister_module(&toi_checksum_ops);
}