xref: /openbmc/linux/kernel/power/swap.c (revision f33ac92f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/kernel/power/swap.c
4  *
5  * This file provides functions for reading the suspend image from
6  * and writing it to a swap partition.
7  *
8  * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
9  * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
10  * Copyright (C) 2010-2012 Bojan Smojver <bojan@rexursive.com>
11  */
12 
13 #define pr_fmt(fmt) "PM: " fmt
14 
15 #include <linux/module.h>
16 #include <linux/file.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/genhd.h>
20 #include <linux/device.h>
21 #include <linux/bio.h>
22 #include <linux/blkdev.h>
23 #include <linux/swap.h>
24 #include <linux/swapops.h>
25 #include <linux/pm.h>
26 #include <linux/slab.h>
27 #include <linux/lzo.h>
28 #include <linux/vmalloc.h>
29 #include <linux/cpumask.h>
30 #include <linux/atomic.h>
31 #include <linux/kthread.h>
32 #include <linux/crc32.h>
33 #include <linux/ktime.h>
34 
35 #include "power.h"
36 
37 #define HIBERNATE_SIG	"S1SUSPEND"
38 
39 u32 swsusp_hardware_signature;
40 
41 /*
42  * When reading an {un,}compressed image, we may restore pages in place,
43  * in which case some architectures need these pages cleaning before they
44  * can be executed. We don't know which pages these may be, so clean the lot.
45  */
46 static bool clean_pages_on_read;
47 static bool clean_pages_on_decompress;
48 
49 /*
50  *	The swap map is a data structure used for keeping track of each page
51  *	written to a swap partition.  It consists of many swap_map_page
52  *	structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
53  *	These structures are stored on the swap and linked together with the
54  *	help of the .next_swap member.
55  *
56  *	The swap map is created during suspend.  The swap map pages are
57  *	allocated and populated one at a time, so we only need one memory
58  *	page to set up the entire structure.
59  *
60  *	During resume we pick up all swap_map_page structures into a list.
61  */
62 
63 #define MAP_PAGE_ENTRIES	(PAGE_SIZE / sizeof(sector_t) - 1)
64 
65 /*
66  * Number of free pages that are not high.
67  */
68 static inline unsigned long low_free_pages(void)
69 {
70 	return nr_free_pages() - nr_free_highpages();
71 }
72 
73 /*
74  * Number of pages required to be kept free while writing the image. Always
75  * half of all available low pages before the writing starts.
76  */
77 static inline unsigned long reqd_free_pages(void)
78 {
79 	return low_free_pages() / 2;
80 }
81 
82 struct swap_map_page {
83 	sector_t entries[MAP_PAGE_ENTRIES];
84 	sector_t next_swap;
85 };
86 
87 struct swap_map_page_list {
88 	struct swap_map_page *map;
89 	struct swap_map_page_list *next;
90 };
91 
92 /**
93  *	The swap_map_handle structure is used for handling swap in
94  *	a file-alike way
95  */
96 
97 struct swap_map_handle {
98 	struct swap_map_page *cur;
99 	struct swap_map_page_list *maps;
100 	sector_t cur_swap;
101 	sector_t first_sector;
102 	unsigned int k;
103 	unsigned long reqd_free_pages;
104 	u32 crc32;
105 };
106 
107 struct swsusp_header {
108 	char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
109 	              sizeof(u32) - sizeof(u32)];
110 	u32	hw_sig;
111 	u32	crc32;
112 	sector_t image;
113 	unsigned int flags;	/* Flags to pass to the "boot" kernel */
114 	char	orig_sig[10];
115 	char	sig[10];
116 } __packed;
117 
118 static struct swsusp_header *swsusp_header;
119 
120 /**
121  *	The following functions are used for tracing the allocated
122  *	swap pages, so that they can be freed in case of an error.
123  */
124 
125 struct swsusp_extent {
126 	struct rb_node node;
127 	unsigned long start;
128 	unsigned long end;
129 };
130 
131 static struct rb_root swsusp_extents = RB_ROOT;
132 
133 static int swsusp_extents_insert(unsigned long swap_offset)
134 {
135 	struct rb_node **new = &(swsusp_extents.rb_node);
136 	struct rb_node *parent = NULL;
137 	struct swsusp_extent *ext;
138 
139 	/* Figure out where to put the new node */
140 	while (*new) {
141 		ext = rb_entry(*new, struct swsusp_extent, node);
142 		parent = *new;
143 		if (swap_offset < ext->start) {
144 			/* Try to merge */
145 			if (swap_offset == ext->start - 1) {
146 				ext->start--;
147 				return 0;
148 			}
149 			new = &((*new)->rb_left);
150 		} else if (swap_offset > ext->end) {
151 			/* Try to merge */
152 			if (swap_offset == ext->end + 1) {
153 				ext->end++;
154 				return 0;
155 			}
156 			new = &((*new)->rb_right);
157 		} else {
158 			/* It already is in the tree */
159 			return -EINVAL;
160 		}
161 	}
162 	/* Add the new node and rebalance the tree. */
163 	ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
164 	if (!ext)
165 		return -ENOMEM;
166 
167 	ext->start = swap_offset;
168 	ext->end = swap_offset;
169 	rb_link_node(&ext->node, parent, new);
170 	rb_insert_color(&ext->node, &swsusp_extents);
171 	return 0;
172 }
173 
174 /**
175  *	alloc_swapdev_block - allocate a swap page and register that it has
176  *	been allocated, so that it can be freed in case of an error.
177  */
178 
179 sector_t alloc_swapdev_block(int swap)
180 {
181 	unsigned long offset;
182 
183 	offset = swp_offset(get_swap_page_of_type(swap));
184 	if (offset) {
185 		if (swsusp_extents_insert(offset))
186 			swap_free(swp_entry(swap, offset));
187 		else
188 			return swapdev_block(swap, offset);
189 	}
190 	return 0;
191 }
192 
193 /**
194  *	free_all_swap_pages - free swap pages allocated for saving image data.
195  *	It also frees the extents used to register which swap entries had been
196  *	allocated.
197  */
198 
199 void free_all_swap_pages(int swap)
200 {
201 	struct rb_node *node;
202 
203 	while ((node = swsusp_extents.rb_node)) {
204 		struct swsusp_extent *ext;
205 		unsigned long offset;
206 
207 		ext = rb_entry(node, struct swsusp_extent, node);
208 		rb_erase(node, &swsusp_extents);
209 		for (offset = ext->start; offset <= ext->end; offset++)
210 			swap_free(swp_entry(swap, offset));
211 
212 		kfree(ext);
213 	}
214 }
215 
216 int swsusp_swap_in_use(void)
217 {
218 	return (swsusp_extents.rb_node != NULL);
219 }
220 
221 /*
222  * General things
223  */
224 
225 static unsigned short root_swap = 0xffff;
226 static struct block_device *hib_resume_bdev;
227 
228 struct hib_bio_batch {
229 	atomic_t		count;
230 	wait_queue_head_t	wait;
231 	blk_status_t		error;
232 	struct blk_plug		plug;
233 };
234 
235 static void hib_init_batch(struct hib_bio_batch *hb)
236 {
237 	atomic_set(&hb->count, 0);
238 	init_waitqueue_head(&hb->wait);
239 	hb->error = BLK_STS_OK;
240 	blk_start_plug(&hb->plug);
241 }
242 
243 static void hib_finish_batch(struct hib_bio_batch *hb)
244 {
245 	blk_finish_plug(&hb->plug);
246 }
247 
248 static void hib_end_io(struct bio *bio)
249 {
250 	struct hib_bio_batch *hb = bio->bi_private;
251 	struct page *page = bio_first_page_all(bio);
252 
253 	if (bio->bi_status) {
254 		pr_alert("Read-error on swap-device (%u:%u:%Lu)\n",
255 			 MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
256 			 (unsigned long long)bio->bi_iter.bi_sector);
257 	}
258 
259 	if (bio_data_dir(bio) == WRITE)
260 		put_page(page);
261 	else if (clean_pages_on_read)
262 		flush_icache_range((unsigned long)page_address(page),
263 				   (unsigned long)page_address(page) + PAGE_SIZE);
264 
265 	if (bio->bi_status && !hb->error)
266 		hb->error = bio->bi_status;
267 	if (atomic_dec_and_test(&hb->count))
268 		wake_up(&hb->wait);
269 
270 	bio_put(bio);
271 }
272 
273 static int hib_submit_io(int op, int op_flags, pgoff_t page_off, void *addr,
274 		struct hib_bio_batch *hb)
275 {
276 	struct page *page = virt_to_page(addr);
277 	struct bio *bio;
278 	int error = 0;
279 
280 	bio = bio_alloc(GFP_NOIO | __GFP_HIGH, 1);
281 	bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
282 	bio_set_dev(bio, hib_resume_bdev);
283 	bio_set_op_attrs(bio, op, op_flags);
284 
285 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
286 		pr_err("Adding page to bio failed at %llu\n",
287 		       (unsigned long long)bio->bi_iter.bi_sector);
288 		bio_put(bio);
289 		return -EFAULT;
290 	}
291 
292 	if (hb) {
293 		bio->bi_end_io = hib_end_io;
294 		bio->bi_private = hb;
295 		atomic_inc(&hb->count);
296 		submit_bio(bio);
297 	} else {
298 		error = submit_bio_wait(bio);
299 		bio_put(bio);
300 	}
301 
302 	return error;
303 }
304 
305 static int hib_wait_io(struct hib_bio_batch *hb)
306 {
307 	/*
308 	 * We are relying on the behavior of blk_plug that a thread with
309 	 * a plug will flush the plug list before sleeping.
310 	 */
311 	wait_event(hb->wait, atomic_read(&hb->count) == 0);
312 	return blk_status_to_errno(hb->error);
313 }
314 
315 /*
316  * Saving part
317  */
318 static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
319 {
320 	int error;
321 
322 	hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
323 		      swsusp_header, NULL);
324 	if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
325 	    !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
326 		memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
327 		memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
328 		swsusp_header->image = handle->first_sector;
329 		if (swsusp_hardware_signature) {
330 			swsusp_header->hw_sig = swsusp_hardware_signature;
331 			flags |= SF_HW_SIG;
332 		}
333 		swsusp_header->flags = flags;
334 		if (flags & SF_CRC32_MODE)
335 			swsusp_header->crc32 = handle->crc32;
336 		error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
337 				      swsusp_resume_block, swsusp_header, NULL);
338 	} else {
339 		pr_err("Swap header not found!\n");
340 		error = -ENODEV;
341 	}
342 	return error;
343 }
344 
345 /**
346  *	swsusp_swap_check - check if the resume device is a swap device
347  *	and get its index (if so)
348  *
349  *	This is called before saving image
350  */
351 static int swsusp_swap_check(void)
352 {
353 	int res;
354 
355 	if (swsusp_resume_device)
356 		res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
357 	else
358 		res = find_first_swap(&swsusp_resume_device);
359 	if (res < 0)
360 		return res;
361 	root_swap = res;
362 
363 	hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device, FMODE_WRITE,
364 			NULL);
365 	if (IS_ERR(hib_resume_bdev))
366 		return PTR_ERR(hib_resume_bdev);
367 
368 	res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
369 	if (res < 0)
370 		blkdev_put(hib_resume_bdev, FMODE_WRITE);
371 
372 	return res;
373 }
374 
375 /**
376  *	write_page - Write one page to given swap location.
377  *	@buf:		Address we're writing.
378  *	@offset:	Offset of the swap page we're writing to.
379  *	@hb:		bio completion batch
380  */
381 
382 static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
383 {
384 	void *src;
385 	int ret;
386 
387 	if (!offset)
388 		return -ENOSPC;
389 
390 	if (hb) {
391 		src = (void *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
392 		                              __GFP_NORETRY);
393 		if (src) {
394 			copy_page(src, buf);
395 		} else {
396 			ret = hib_wait_io(hb); /* Free pages */
397 			if (ret)
398 				return ret;
399 			src = (void *)__get_free_page(GFP_NOIO |
400 			                              __GFP_NOWARN |
401 			                              __GFP_NORETRY);
402 			if (src) {
403 				copy_page(src, buf);
404 			} else {
405 				WARN_ON_ONCE(1);
406 				hb = NULL;	/* Go synchronous */
407 				src = buf;
408 			}
409 		}
410 	} else {
411 		src = buf;
412 	}
413 	return hib_submit_io(REQ_OP_WRITE, REQ_SYNC, offset, src, hb);
414 }
415 
416 static void release_swap_writer(struct swap_map_handle *handle)
417 {
418 	if (handle->cur)
419 		free_page((unsigned long)handle->cur);
420 	handle->cur = NULL;
421 }
422 
423 static int get_swap_writer(struct swap_map_handle *handle)
424 {
425 	int ret;
426 
427 	ret = swsusp_swap_check();
428 	if (ret) {
429 		if (ret != -ENOSPC)
430 			pr_err("Cannot find swap device, try swapon -a\n");
431 		return ret;
432 	}
433 	handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
434 	if (!handle->cur) {
435 		ret = -ENOMEM;
436 		goto err_close;
437 	}
438 	handle->cur_swap = alloc_swapdev_block(root_swap);
439 	if (!handle->cur_swap) {
440 		ret = -ENOSPC;
441 		goto err_rel;
442 	}
443 	handle->k = 0;
444 	handle->reqd_free_pages = reqd_free_pages();
445 	handle->first_sector = handle->cur_swap;
446 	return 0;
447 err_rel:
448 	release_swap_writer(handle);
449 err_close:
450 	swsusp_close(FMODE_WRITE);
451 	return ret;
452 }
453 
454 static int swap_write_page(struct swap_map_handle *handle, void *buf,
455 		struct hib_bio_batch *hb)
456 {
457 	int error = 0;
458 	sector_t offset;
459 
460 	if (!handle->cur)
461 		return -EINVAL;
462 	offset = alloc_swapdev_block(root_swap);
463 	error = write_page(buf, offset, hb);
464 	if (error)
465 		return error;
466 	handle->cur->entries[handle->k++] = offset;
467 	if (handle->k >= MAP_PAGE_ENTRIES) {
468 		offset = alloc_swapdev_block(root_swap);
469 		if (!offset)
470 			return -ENOSPC;
471 		handle->cur->next_swap = offset;
472 		error = write_page(handle->cur, handle->cur_swap, hb);
473 		if (error)
474 			goto out;
475 		clear_page(handle->cur);
476 		handle->cur_swap = offset;
477 		handle->k = 0;
478 
479 		if (hb && low_free_pages() <= handle->reqd_free_pages) {
480 			error = hib_wait_io(hb);
481 			if (error)
482 				goto out;
483 			/*
484 			 * Recalculate the number of required free pages, to
485 			 * make sure we never take more than half.
486 			 */
487 			handle->reqd_free_pages = reqd_free_pages();
488 		}
489 	}
490  out:
491 	return error;
492 }
493 
494 static int flush_swap_writer(struct swap_map_handle *handle)
495 {
496 	if (handle->cur && handle->cur_swap)
497 		return write_page(handle->cur, handle->cur_swap, NULL);
498 	else
499 		return -EINVAL;
500 }
501 
502 static int swap_writer_finish(struct swap_map_handle *handle,
503 		unsigned int flags, int error)
504 {
505 	if (!error) {
506 		pr_info("S");
507 		error = mark_swapfiles(handle, flags);
508 		pr_cont("|\n");
509 		flush_swap_writer(handle);
510 	}
511 
512 	if (error)
513 		free_all_swap_pages(root_swap);
514 	release_swap_writer(handle);
515 	swsusp_close(FMODE_WRITE);
516 
517 	return error;
518 }
519 
520 /* We need to remember how much compressed data we need to read. */
521 #define LZO_HEADER	sizeof(size_t)
522 
523 /* Number of pages/bytes we'll compress at one time. */
524 #define LZO_UNC_PAGES	32
525 #define LZO_UNC_SIZE	(LZO_UNC_PAGES * PAGE_SIZE)
526 
527 /* Number of pages/bytes we need for compressed data (worst case). */
528 #define LZO_CMP_PAGES	DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
529 			             LZO_HEADER, PAGE_SIZE)
530 #define LZO_CMP_SIZE	(LZO_CMP_PAGES * PAGE_SIZE)
531 
532 /* Maximum number of threads for compression/decompression. */
533 #define LZO_THREADS	3
534 
535 /* Minimum/maximum number of pages for read buffering. */
536 #define LZO_MIN_RD_PAGES	1024
537 #define LZO_MAX_RD_PAGES	8192
538 
539 
540 /**
541  *	save_image - save the suspend image data
542  */
543 
544 static int save_image(struct swap_map_handle *handle,
545                       struct snapshot_handle *snapshot,
546                       unsigned int nr_to_write)
547 {
548 	unsigned int m;
549 	int ret;
550 	int nr_pages;
551 	int err2;
552 	struct hib_bio_batch hb;
553 	ktime_t start;
554 	ktime_t stop;
555 
556 	hib_init_batch(&hb);
557 
558 	pr_info("Saving image data pages (%u pages)...\n",
559 		nr_to_write);
560 	m = nr_to_write / 10;
561 	if (!m)
562 		m = 1;
563 	nr_pages = 0;
564 	start = ktime_get();
565 	while (1) {
566 		ret = snapshot_read_next(snapshot);
567 		if (ret <= 0)
568 			break;
569 		ret = swap_write_page(handle, data_of(*snapshot), &hb);
570 		if (ret)
571 			break;
572 		if (!(nr_pages % m))
573 			pr_info("Image saving progress: %3d%%\n",
574 				nr_pages / m * 10);
575 		nr_pages++;
576 	}
577 	err2 = hib_wait_io(&hb);
578 	hib_finish_batch(&hb);
579 	stop = ktime_get();
580 	if (!ret)
581 		ret = err2;
582 	if (!ret)
583 		pr_info("Image saving done\n");
584 	swsusp_show_speed(start, stop, nr_to_write, "Wrote");
585 	return ret;
586 }
587 
588 /**
589  * Structure used for CRC32.
590  */
591 struct crc_data {
592 	struct task_struct *thr;                  /* thread */
593 	atomic_t ready;                           /* ready to start flag */
594 	atomic_t stop;                            /* ready to stop flag */
595 	unsigned run_threads;                     /* nr current threads */
596 	wait_queue_head_t go;                     /* start crc update */
597 	wait_queue_head_t done;                   /* crc update done */
598 	u32 *crc32;                               /* points to handle's crc32 */
599 	size_t *unc_len[LZO_THREADS];             /* uncompressed lengths */
600 	unsigned char *unc[LZO_THREADS];          /* uncompressed data */
601 };
602 
603 /**
604  * CRC32 update function that runs in its own thread.
605  */
606 static int crc32_threadfn(void *data)
607 {
608 	struct crc_data *d = data;
609 	unsigned i;
610 
611 	while (1) {
612 		wait_event(d->go, atomic_read(&d->ready) ||
613 		                  kthread_should_stop());
614 		if (kthread_should_stop()) {
615 			d->thr = NULL;
616 			atomic_set(&d->stop, 1);
617 			wake_up(&d->done);
618 			break;
619 		}
620 		atomic_set(&d->ready, 0);
621 
622 		for (i = 0; i < d->run_threads; i++)
623 			*d->crc32 = crc32_le(*d->crc32,
624 			                     d->unc[i], *d->unc_len[i]);
625 		atomic_set(&d->stop, 1);
626 		wake_up(&d->done);
627 	}
628 	return 0;
629 }
630 /**
631  * Structure used for LZO data compression.
632  */
633 struct cmp_data {
634 	struct task_struct *thr;                  /* thread */
635 	atomic_t ready;                           /* ready to start flag */
636 	atomic_t stop;                            /* ready to stop flag */
637 	int ret;                                  /* return code */
638 	wait_queue_head_t go;                     /* start compression */
639 	wait_queue_head_t done;                   /* compression done */
640 	size_t unc_len;                           /* uncompressed length */
641 	size_t cmp_len;                           /* compressed length */
642 	unsigned char unc[LZO_UNC_SIZE];          /* uncompressed buffer */
643 	unsigned char cmp[LZO_CMP_SIZE];          /* compressed buffer */
644 	unsigned char wrk[LZO1X_1_MEM_COMPRESS];  /* compression workspace */
645 };
646 
647 /**
648  * Compression function that runs in its own thread.
649  */
650 static int lzo_compress_threadfn(void *data)
651 {
652 	struct cmp_data *d = data;
653 
654 	while (1) {
655 		wait_event(d->go, atomic_read(&d->ready) ||
656 		                  kthread_should_stop());
657 		if (kthread_should_stop()) {
658 			d->thr = NULL;
659 			d->ret = -1;
660 			atomic_set(&d->stop, 1);
661 			wake_up(&d->done);
662 			break;
663 		}
664 		atomic_set(&d->ready, 0);
665 
666 		d->ret = lzo1x_1_compress(d->unc, d->unc_len,
667 		                          d->cmp + LZO_HEADER, &d->cmp_len,
668 		                          d->wrk);
669 		atomic_set(&d->stop, 1);
670 		wake_up(&d->done);
671 	}
672 	return 0;
673 }
674 
675 /**
676  * save_image_lzo - Save the suspend image data compressed with LZO.
677  * @handle: Swap map handle to use for saving the image.
678  * @snapshot: Image to read data from.
679  * @nr_to_write: Number of pages to save.
680  */
681 static int save_image_lzo(struct swap_map_handle *handle,
682                           struct snapshot_handle *snapshot,
683                           unsigned int nr_to_write)
684 {
685 	unsigned int m;
686 	int ret = 0;
687 	int nr_pages;
688 	int err2;
689 	struct hib_bio_batch hb;
690 	ktime_t start;
691 	ktime_t stop;
692 	size_t off;
693 	unsigned thr, run_threads, nr_threads;
694 	unsigned char *page = NULL;
695 	struct cmp_data *data = NULL;
696 	struct crc_data *crc = NULL;
697 
698 	hib_init_batch(&hb);
699 
700 	/*
701 	 * We'll limit the number of threads for compression to limit memory
702 	 * footprint.
703 	 */
704 	nr_threads = num_online_cpus() - 1;
705 	nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
706 
707 	page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
708 	if (!page) {
709 		pr_err("Failed to allocate LZO page\n");
710 		ret = -ENOMEM;
711 		goto out_clean;
712 	}
713 
714 	data = vzalloc(array_size(nr_threads, sizeof(*data)));
715 	if (!data) {
716 		pr_err("Failed to allocate LZO data\n");
717 		ret = -ENOMEM;
718 		goto out_clean;
719 	}
720 
721 	crc = kzalloc(sizeof(*crc), GFP_KERNEL);
722 	if (!crc) {
723 		pr_err("Failed to allocate crc\n");
724 		ret = -ENOMEM;
725 		goto out_clean;
726 	}
727 
728 	/*
729 	 * Start the compression threads.
730 	 */
731 	for (thr = 0; thr < nr_threads; thr++) {
732 		init_waitqueue_head(&data[thr].go);
733 		init_waitqueue_head(&data[thr].done);
734 
735 		data[thr].thr = kthread_run(lzo_compress_threadfn,
736 		                            &data[thr],
737 		                            "image_compress/%u", thr);
738 		if (IS_ERR(data[thr].thr)) {
739 			data[thr].thr = NULL;
740 			pr_err("Cannot start compression threads\n");
741 			ret = -ENOMEM;
742 			goto out_clean;
743 		}
744 	}
745 
746 	/*
747 	 * Start the CRC32 thread.
748 	 */
749 	init_waitqueue_head(&crc->go);
750 	init_waitqueue_head(&crc->done);
751 
752 	handle->crc32 = 0;
753 	crc->crc32 = &handle->crc32;
754 	for (thr = 0; thr < nr_threads; thr++) {
755 		crc->unc[thr] = data[thr].unc;
756 		crc->unc_len[thr] = &data[thr].unc_len;
757 	}
758 
759 	crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
760 	if (IS_ERR(crc->thr)) {
761 		crc->thr = NULL;
762 		pr_err("Cannot start CRC32 thread\n");
763 		ret = -ENOMEM;
764 		goto out_clean;
765 	}
766 
767 	/*
768 	 * Adjust the number of required free pages after all allocations have
769 	 * been done. We don't want to run out of pages when writing.
770 	 */
771 	handle->reqd_free_pages = reqd_free_pages();
772 
773 	pr_info("Using %u thread(s) for compression\n", nr_threads);
774 	pr_info("Compressing and saving image data (%u pages)...\n",
775 		nr_to_write);
776 	m = nr_to_write / 10;
777 	if (!m)
778 		m = 1;
779 	nr_pages = 0;
780 	start = ktime_get();
781 	for (;;) {
782 		for (thr = 0; thr < nr_threads; thr++) {
783 			for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
784 				ret = snapshot_read_next(snapshot);
785 				if (ret < 0)
786 					goto out_finish;
787 
788 				if (!ret)
789 					break;
790 
791 				memcpy(data[thr].unc + off,
792 				       data_of(*snapshot), PAGE_SIZE);
793 
794 				if (!(nr_pages % m))
795 					pr_info("Image saving progress: %3d%%\n",
796 						nr_pages / m * 10);
797 				nr_pages++;
798 			}
799 			if (!off)
800 				break;
801 
802 			data[thr].unc_len = off;
803 
804 			atomic_set(&data[thr].ready, 1);
805 			wake_up(&data[thr].go);
806 		}
807 
808 		if (!thr)
809 			break;
810 
811 		crc->run_threads = thr;
812 		atomic_set(&crc->ready, 1);
813 		wake_up(&crc->go);
814 
815 		for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
816 			wait_event(data[thr].done,
817 			           atomic_read(&data[thr].stop));
818 			atomic_set(&data[thr].stop, 0);
819 
820 			ret = data[thr].ret;
821 
822 			if (ret < 0) {
823 				pr_err("LZO compression failed\n");
824 				goto out_finish;
825 			}
826 
827 			if (unlikely(!data[thr].cmp_len ||
828 			             data[thr].cmp_len >
829 			             lzo1x_worst_compress(data[thr].unc_len))) {
830 				pr_err("Invalid LZO compressed length\n");
831 				ret = -1;
832 				goto out_finish;
833 			}
834 
835 			*(size_t *)data[thr].cmp = data[thr].cmp_len;
836 
837 			/*
838 			 * Given we are writing one page at a time to disk, we
839 			 * copy that much from the buffer, although the last
840 			 * bit will likely be smaller than full page. This is
841 			 * OK - we saved the length of the compressed data, so
842 			 * any garbage at the end will be discarded when we
843 			 * read it.
844 			 */
845 			for (off = 0;
846 			     off < LZO_HEADER + data[thr].cmp_len;
847 			     off += PAGE_SIZE) {
848 				memcpy(page, data[thr].cmp + off, PAGE_SIZE);
849 
850 				ret = swap_write_page(handle, page, &hb);
851 				if (ret)
852 					goto out_finish;
853 			}
854 		}
855 
856 		wait_event(crc->done, atomic_read(&crc->stop));
857 		atomic_set(&crc->stop, 0);
858 	}
859 
860 out_finish:
861 	err2 = hib_wait_io(&hb);
862 	stop = ktime_get();
863 	if (!ret)
864 		ret = err2;
865 	if (!ret)
866 		pr_info("Image saving done\n");
867 	swsusp_show_speed(start, stop, nr_to_write, "Wrote");
868 out_clean:
869 	hib_finish_batch(&hb);
870 	if (crc) {
871 		if (crc->thr)
872 			kthread_stop(crc->thr);
873 		kfree(crc);
874 	}
875 	if (data) {
876 		for (thr = 0; thr < nr_threads; thr++)
877 			if (data[thr].thr)
878 				kthread_stop(data[thr].thr);
879 		vfree(data);
880 	}
881 	if (page) free_page((unsigned long)page);
882 
883 	return ret;
884 }
885 
886 /**
887  *	enough_swap - Make sure we have enough swap to save the image.
888  *
889  *	Returns TRUE or FALSE after checking the total amount of swap
890  *	space available from the resume partition.
891  */
892 
893 static int enough_swap(unsigned int nr_pages)
894 {
895 	unsigned int free_swap = count_swap_pages(root_swap, 1);
896 	unsigned int required;
897 
898 	pr_debug("Free swap pages: %u\n", free_swap);
899 
900 	required = PAGES_FOR_IO + nr_pages;
901 	return free_swap > required;
902 }
903 
904 /**
905  *	swsusp_write - Write entire image and metadata.
906  *	@flags: flags to pass to the "boot" kernel in the image header
907  *
908  *	It is important _NOT_ to umount filesystems at this point. We want
909  *	them synced (in case something goes wrong) but we DO not want to mark
910  *	filesystem clean: it is not. (And it does not matter, if we resume
911  *	correctly, we'll mark system clean, anyway.)
912  */
913 
914 int swsusp_write(unsigned int flags)
915 {
916 	struct swap_map_handle handle;
917 	struct snapshot_handle snapshot;
918 	struct swsusp_info *header;
919 	unsigned long pages;
920 	int error;
921 
922 	pages = snapshot_get_image_size();
923 	error = get_swap_writer(&handle);
924 	if (error) {
925 		pr_err("Cannot get swap writer\n");
926 		return error;
927 	}
928 	if (flags & SF_NOCOMPRESS_MODE) {
929 		if (!enough_swap(pages)) {
930 			pr_err("Not enough free swap\n");
931 			error = -ENOSPC;
932 			goto out_finish;
933 		}
934 	}
935 	memset(&snapshot, 0, sizeof(struct snapshot_handle));
936 	error = snapshot_read_next(&snapshot);
937 	if (error < (int)PAGE_SIZE) {
938 		if (error >= 0)
939 			error = -EFAULT;
940 
941 		goto out_finish;
942 	}
943 	header = (struct swsusp_info *)data_of(snapshot);
944 	error = swap_write_page(&handle, header, NULL);
945 	if (!error) {
946 		error = (flags & SF_NOCOMPRESS_MODE) ?
947 			save_image(&handle, &snapshot, pages - 1) :
948 			save_image_lzo(&handle, &snapshot, pages - 1);
949 	}
950 out_finish:
951 	error = swap_writer_finish(&handle, flags, error);
952 	return error;
953 }
954 
955 /**
956  *	The following functions allow us to read data using a swap map
957  *	in a file-alike way
958  */
959 
960 static void release_swap_reader(struct swap_map_handle *handle)
961 {
962 	struct swap_map_page_list *tmp;
963 
964 	while (handle->maps) {
965 		if (handle->maps->map)
966 			free_page((unsigned long)handle->maps->map);
967 		tmp = handle->maps;
968 		handle->maps = handle->maps->next;
969 		kfree(tmp);
970 	}
971 	handle->cur = NULL;
972 }
973 
974 static int get_swap_reader(struct swap_map_handle *handle,
975 		unsigned int *flags_p)
976 {
977 	int error;
978 	struct swap_map_page_list *tmp, *last;
979 	sector_t offset;
980 
981 	*flags_p = swsusp_header->flags;
982 
983 	if (!swsusp_header->image) /* how can this happen? */
984 		return -EINVAL;
985 
986 	handle->cur = NULL;
987 	last = handle->maps = NULL;
988 	offset = swsusp_header->image;
989 	while (offset) {
990 		tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
991 		if (!tmp) {
992 			release_swap_reader(handle);
993 			return -ENOMEM;
994 		}
995 		if (!handle->maps)
996 			handle->maps = tmp;
997 		if (last)
998 			last->next = tmp;
999 		last = tmp;
1000 
1001 		tmp->map = (struct swap_map_page *)
1002 			   __get_free_page(GFP_NOIO | __GFP_HIGH);
1003 		if (!tmp->map) {
1004 			release_swap_reader(handle);
1005 			return -ENOMEM;
1006 		}
1007 
1008 		error = hib_submit_io(REQ_OP_READ, 0, offset, tmp->map, NULL);
1009 		if (error) {
1010 			release_swap_reader(handle);
1011 			return error;
1012 		}
1013 		offset = tmp->map->next_swap;
1014 	}
1015 	handle->k = 0;
1016 	handle->cur = handle->maps->map;
1017 	return 0;
1018 }
1019 
1020 static int swap_read_page(struct swap_map_handle *handle, void *buf,
1021 		struct hib_bio_batch *hb)
1022 {
1023 	sector_t offset;
1024 	int error;
1025 	struct swap_map_page_list *tmp;
1026 
1027 	if (!handle->cur)
1028 		return -EINVAL;
1029 	offset = handle->cur->entries[handle->k];
1030 	if (!offset)
1031 		return -EFAULT;
1032 	error = hib_submit_io(REQ_OP_READ, 0, offset, buf, hb);
1033 	if (error)
1034 		return error;
1035 	if (++handle->k >= MAP_PAGE_ENTRIES) {
1036 		handle->k = 0;
1037 		free_page((unsigned long)handle->maps->map);
1038 		tmp = handle->maps;
1039 		handle->maps = handle->maps->next;
1040 		kfree(tmp);
1041 		if (!handle->maps)
1042 			release_swap_reader(handle);
1043 		else
1044 			handle->cur = handle->maps->map;
1045 	}
1046 	return error;
1047 }
1048 
1049 static int swap_reader_finish(struct swap_map_handle *handle)
1050 {
1051 	release_swap_reader(handle);
1052 
1053 	return 0;
1054 }
1055 
1056 /**
1057  *	load_image - load the image using the swap map handle
1058  *	@handle and the snapshot handle @snapshot
1059  *	(assume there are @nr_pages pages to load)
1060  */
1061 
1062 static int load_image(struct swap_map_handle *handle,
1063                       struct snapshot_handle *snapshot,
1064                       unsigned int nr_to_read)
1065 {
1066 	unsigned int m;
1067 	int ret = 0;
1068 	ktime_t start;
1069 	ktime_t stop;
1070 	struct hib_bio_batch hb;
1071 	int err2;
1072 	unsigned nr_pages;
1073 
1074 	hib_init_batch(&hb);
1075 
1076 	clean_pages_on_read = true;
1077 	pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
1078 	m = nr_to_read / 10;
1079 	if (!m)
1080 		m = 1;
1081 	nr_pages = 0;
1082 	start = ktime_get();
1083 	for ( ; ; ) {
1084 		ret = snapshot_write_next(snapshot);
1085 		if (ret <= 0)
1086 			break;
1087 		ret = swap_read_page(handle, data_of(*snapshot), &hb);
1088 		if (ret)
1089 			break;
1090 		if (snapshot->sync_read)
1091 			ret = hib_wait_io(&hb);
1092 		if (ret)
1093 			break;
1094 		if (!(nr_pages % m))
1095 			pr_info("Image loading progress: %3d%%\n",
1096 				nr_pages / m * 10);
1097 		nr_pages++;
1098 	}
1099 	err2 = hib_wait_io(&hb);
1100 	hib_finish_batch(&hb);
1101 	stop = ktime_get();
1102 	if (!ret)
1103 		ret = err2;
1104 	if (!ret) {
1105 		pr_info("Image loading done\n");
1106 		snapshot_write_finalize(snapshot);
1107 		if (!snapshot_image_loaded(snapshot))
1108 			ret = -ENODATA;
1109 	}
1110 	swsusp_show_speed(start, stop, nr_to_read, "Read");
1111 	return ret;
1112 }
1113 
1114 /**
1115  * Structure used for LZO data decompression.
1116  */
1117 struct dec_data {
1118 	struct task_struct *thr;                  /* thread */
1119 	atomic_t ready;                           /* ready to start flag */
1120 	atomic_t stop;                            /* ready to stop flag */
1121 	int ret;                                  /* return code */
1122 	wait_queue_head_t go;                     /* start decompression */
1123 	wait_queue_head_t done;                   /* decompression done */
1124 	size_t unc_len;                           /* uncompressed length */
1125 	size_t cmp_len;                           /* compressed length */
1126 	unsigned char unc[LZO_UNC_SIZE];          /* uncompressed buffer */
1127 	unsigned char cmp[LZO_CMP_SIZE];          /* compressed buffer */
1128 };
1129 
1130 /**
1131  * Decompression function that runs in its own thread.
1132  */
1133 static int lzo_decompress_threadfn(void *data)
1134 {
1135 	struct dec_data *d = data;
1136 
1137 	while (1) {
1138 		wait_event(d->go, atomic_read(&d->ready) ||
1139 		                  kthread_should_stop());
1140 		if (kthread_should_stop()) {
1141 			d->thr = NULL;
1142 			d->ret = -1;
1143 			atomic_set(&d->stop, 1);
1144 			wake_up(&d->done);
1145 			break;
1146 		}
1147 		atomic_set(&d->ready, 0);
1148 
1149 		d->unc_len = LZO_UNC_SIZE;
1150 		d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
1151 		                               d->unc, &d->unc_len);
1152 		if (clean_pages_on_decompress)
1153 			flush_icache_range((unsigned long)d->unc,
1154 					   (unsigned long)d->unc + d->unc_len);
1155 
1156 		atomic_set(&d->stop, 1);
1157 		wake_up(&d->done);
1158 	}
1159 	return 0;
1160 }
1161 
1162 /**
1163  * load_image_lzo - Load compressed image data and decompress them with LZO.
1164  * @handle: Swap map handle to use for loading data.
1165  * @snapshot: Image to copy uncompressed data into.
1166  * @nr_to_read: Number of pages to load.
1167  */
1168 static int load_image_lzo(struct swap_map_handle *handle,
1169                           struct snapshot_handle *snapshot,
1170                           unsigned int nr_to_read)
1171 {
1172 	unsigned int m;
1173 	int ret = 0;
1174 	int eof = 0;
1175 	struct hib_bio_batch hb;
1176 	ktime_t start;
1177 	ktime_t stop;
1178 	unsigned nr_pages;
1179 	size_t off;
1180 	unsigned i, thr, run_threads, nr_threads;
1181 	unsigned ring = 0, pg = 0, ring_size = 0,
1182 	         have = 0, want, need, asked = 0;
1183 	unsigned long read_pages = 0;
1184 	unsigned char **page = NULL;
1185 	struct dec_data *data = NULL;
1186 	struct crc_data *crc = NULL;
1187 
1188 	hib_init_batch(&hb);
1189 
1190 	/*
1191 	 * We'll limit the number of threads for decompression to limit memory
1192 	 * footprint.
1193 	 */
1194 	nr_threads = num_online_cpus() - 1;
1195 	nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
1196 
1197 	page = vmalloc(array_size(LZO_MAX_RD_PAGES, sizeof(*page)));
1198 	if (!page) {
1199 		pr_err("Failed to allocate LZO page\n");
1200 		ret = -ENOMEM;
1201 		goto out_clean;
1202 	}
1203 
1204 	data = vzalloc(array_size(nr_threads, sizeof(*data)));
1205 	if (!data) {
1206 		pr_err("Failed to allocate LZO data\n");
1207 		ret = -ENOMEM;
1208 		goto out_clean;
1209 	}
1210 
1211 	crc = kzalloc(sizeof(*crc), GFP_KERNEL);
1212 	if (!crc) {
1213 		pr_err("Failed to allocate crc\n");
1214 		ret = -ENOMEM;
1215 		goto out_clean;
1216 	}
1217 
1218 	clean_pages_on_decompress = true;
1219 
1220 	/*
1221 	 * Start the decompression threads.
1222 	 */
1223 	for (thr = 0; thr < nr_threads; thr++) {
1224 		init_waitqueue_head(&data[thr].go);
1225 		init_waitqueue_head(&data[thr].done);
1226 
1227 		data[thr].thr = kthread_run(lzo_decompress_threadfn,
1228 		                            &data[thr],
1229 		                            "image_decompress/%u", thr);
1230 		if (IS_ERR(data[thr].thr)) {
1231 			data[thr].thr = NULL;
1232 			pr_err("Cannot start decompression threads\n");
1233 			ret = -ENOMEM;
1234 			goto out_clean;
1235 		}
1236 	}
1237 
1238 	/*
1239 	 * Start the CRC32 thread.
1240 	 */
1241 	init_waitqueue_head(&crc->go);
1242 	init_waitqueue_head(&crc->done);
1243 
1244 	handle->crc32 = 0;
1245 	crc->crc32 = &handle->crc32;
1246 	for (thr = 0; thr < nr_threads; thr++) {
1247 		crc->unc[thr] = data[thr].unc;
1248 		crc->unc_len[thr] = &data[thr].unc_len;
1249 	}
1250 
1251 	crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1252 	if (IS_ERR(crc->thr)) {
1253 		crc->thr = NULL;
1254 		pr_err("Cannot start CRC32 thread\n");
1255 		ret = -ENOMEM;
1256 		goto out_clean;
1257 	}
1258 
1259 	/*
1260 	 * Set the number of pages for read buffering.
1261 	 * This is complete guesswork, because we'll only know the real
1262 	 * picture once prepare_image() is called, which is much later on
1263 	 * during the image load phase. We'll assume the worst case and
1264 	 * say that none of the image pages are from high memory.
1265 	 */
1266 	if (low_free_pages() > snapshot_get_image_size())
1267 		read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1268 	read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES);
1269 
1270 	for (i = 0; i < read_pages; i++) {
1271 		page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
1272 						  GFP_NOIO | __GFP_HIGH :
1273 						  GFP_NOIO | __GFP_NOWARN |
1274 						  __GFP_NORETRY);
1275 
1276 		if (!page[i]) {
1277 			if (i < LZO_CMP_PAGES) {
1278 				ring_size = i;
1279 				pr_err("Failed to allocate LZO pages\n");
1280 				ret = -ENOMEM;
1281 				goto out_clean;
1282 			} else {
1283 				break;
1284 			}
1285 		}
1286 	}
1287 	want = ring_size = i;
1288 
1289 	pr_info("Using %u thread(s) for decompression\n", nr_threads);
1290 	pr_info("Loading and decompressing image data (%u pages)...\n",
1291 		nr_to_read);
1292 	m = nr_to_read / 10;
1293 	if (!m)
1294 		m = 1;
1295 	nr_pages = 0;
1296 	start = ktime_get();
1297 
1298 	ret = snapshot_write_next(snapshot);
1299 	if (ret <= 0)
1300 		goto out_finish;
1301 
1302 	for(;;) {
1303 		for (i = 0; !eof && i < want; i++) {
1304 			ret = swap_read_page(handle, page[ring], &hb);
1305 			if (ret) {
1306 				/*
1307 				 * On real read error, finish. On end of data,
1308 				 * set EOF flag and just exit the read loop.
1309 				 */
1310 				if (handle->cur &&
1311 				    handle->cur->entries[handle->k]) {
1312 					goto out_finish;
1313 				} else {
1314 					eof = 1;
1315 					break;
1316 				}
1317 			}
1318 			if (++ring >= ring_size)
1319 				ring = 0;
1320 		}
1321 		asked += i;
1322 		want -= i;
1323 
1324 		/*
1325 		 * We are out of data, wait for some more.
1326 		 */
1327 		if (!have) {
1328 			if (!asked)
1329 				break;
1330 
1331 			ret = hib_wait_io(&hb);
1332 			if (ret)
1333 				goto out_finish;
1334 			have += asked;
1335 			asked = 0;
1336 			if (eof)
1337 				eof = 2;
1338 		}
1339 
1340 		if (crc->run_threads) {
1341 			wait_event(crc->done, atomic_read(&crc->stop));
1342 			atomic_set(&crc->stop, 0);
1343 			crc->run_threads = 0;
1344 		}
1345 
1346 		for (thr = 0; have && thr < nr_threads; thr++) {
1347 			data[thr].cmp_len = *(size_t *)page[pg];
1348 			if (unlikely(!data[thr].cmp_len ||
1349 			             data[thr].cmp_len >
1350 			             lzo1x_worst_compress(LZO_UNC_SIZE))) {
1351 				pr_err("Invalid LZO compressed length\n");
1352 				ret = -1;
1353 				goto out_finish;
1354 			}
1355 
1356 			need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
1357 			                    PAGE_SIZE);
1358 			if (need > have) {
1359 				if (eof > 1) {
1360 					ret = -1;
1361 					goto out_finish;
1362 				}
1363 				break;
1364 			}
1365 
1366 			for (off = 0;
1367 			     off < LZO_HEADER + data[thr].cmp_len;
1368 			     off += PAGE_SIZE) {
1369 				memcpy(data[thr].cmp + off,
1370 				       page[pg], PAGE_SIZE);
1371 				have--;
1372 				want++;
1373 				if (++pg >= ring_size)
1374 					pg = 0;
1375 			}
1376 
1377 			atomic_set(&data[thr].ready, 1);
1378 			wake_up(&data[thr].go);
1379 		}
1380 
1381 		/*
1382 		 * Wait for more data while we are decompressing.
1383 		 */
1384 		if (have < LZO_CMP_PAGES && asked) {
1385 			ret = hib_wait_io(&hb);
1386 			if (ret)
1387 				goto out_finish;
1388 			have += asked;
1389 			asked = 0;
1390 			if (eof)
1391 				eof = 2;
1392 		}
1393 
1394 		for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1395 			wait_event(data[thr].done,
1396 			           atomic_read(&data[thr].stop));
1397 			atomic_set(&data[thr].stop, 0);
1398 
1399 			ret = data[thr].ret;
1400 
1401 			if (ret < 0) {
1402 				pr_err("LZO decompression failed\n");
1403 				goto out_finish;
1404 			}
1405 
1406 			if (unlikely(!data[thr].unc_len ||
1407 			             data[thr].unc_len > LZO_UNC_SIZE ||
1408 			             data[thr].unc_len & (PAGE_SIZE - 1))) {
1409 				pr_err("Invalid LZO uncompressed length\n");
1410 				ret = -1;
1411 				goto out_finish;
1412 			}
1413 
1414 			for (off = 0;
1415 			     off < data[thr].unc_len; off += PAGE_SIZE) {
1416 				memcpy(data_of(*snapshot),
1417 				       data[thr].unc + off, PAGE_SIZE);
1418 
1419 				if (!(nr_pages % m))
1420 					pr_info("Image loading progress: %3d%%\n",
1421 						nr_pages / m * 10);
1422 				nr_pages++;
1423 
1424 				ret = snapshot_write_next(snapshot);
1425 				if (ret <= 0) {
1426 					crc->run_threads = thr + 1;
1427 					atomic_set(&crc->ready, 1);
1428 					wake_up(&crc->go);
1429 					goto out_finish;
1430 				}
1431 			}
1432 		}
1433 
1434 		crc->run_threads = thr;
1435 		atomic_set(&crc->ready, 1);
1436 		wake_up(&crc->go);
1437 	}
1438 
1439 out_finish:
1440 	if (crc->run_threads) {
1441 		wait_event(crc->done, atomic_read(&crc->stop));
1442 		atomic_set(&crc->stop, 0);
1443 	}
1444 	stop = ktime_get();
1445 	if (!ret) {
1446 		pr_info("Image loading done\n");
1447 		snapshot_write_finalize(snapshot);
1448 		if (!snapshot_image_loaded(snapshot))
1449 			ret = -ENODATA;
1450 		if (!ret) {
1451 			if (swsusp_header->flags & SF_CRC32_MODE) {
1452 				if(handle->crc32 != swsusp_header->crc32) {
1453 					pr_err("Invalid image CRC32!\n");
1454 					ret = -ENODATA;
1455 				}
1456 			}
1457 		}
1458 	}
1459 	swsusp_show_speed(start, stop, nr_to_read, "Read");
1460 out_clean:
1461 	hib_finish_batch(&hb);
1462 	for (i = 0; i < ring_size; i++)
1463 		free_page((unsigned long)page[i]);
1464 	if (crc) {
1465 		if (crc->thr)
1466 			kthread_stop(crc->thr);
1467 		kfree(crc);
1468 	}
1469 	if (data) {
1470 		for (thr = 0; thr < nr_threads; thr++)
1471 			if (data[thr].thr)
1472 				kthread_stop(data[thr].thr);
1473 		vfree(data);
1474 	}
1475 	vfree(page);
1476 
1477 	return ret;
1478 }
1479 
1480 /**
1481  *	swsusp_read - read the hibernation image.
1482  *	@flags_p: flags passed by the "frozen" kernel in the image header should
1483  *		  be written into this memory location
1484  */
1485 
1486 int swsusp_read(unsigned int *flags_p)
1487 {
1488 	int error;
1489 	struct swap_map_handle handle;
1490 	struct snapshot_handle snapshot;
1491 	struct swsusp_info *header;
1492 
1493 	memset(&snapshot, 0, sizeof(struct snapshot_handle));
1494 	error = snapshot_write_next(&snapshot);
1495 	if (error < (int)PAGE_SIZE)
1496 		return error < 0 ? error : -EFAULT;
1497 	header = (struct swsusp_info *)data_of(snapshot);
1498 	error = get_swap_reader(&handle, flags_p);
1499 	if (error)
1500 		goto end;
1501 	if (!error)
1502 		error = swap_read_page(&handle, header, NULL);
1503 	if (!error) {
1504 		error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1505 			load_image(&handle, &snapshot, header->pages - 1) :
1506 			load_image_lzo(&handle, &snapshot, header->pages - 1);
1507 	}
1508 	swap_reader_finish(&handle);
1509 end:
1510 	if (!error)
1511 		pr_debug("Image successfully loaded\n");
1512 	else
1513 		pr_debug("Error %d resuming\n", error);
1514 	return error;
1515 }
1516 
1517 /**
1518  *      swsusp_check - Check for swsusp signature in the resume device
1519  */
1520 
1521 int swsusp_check(void)
1522 {
1523 	int error;
1524 	void *holder;
1525 
1526 	hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
1527 					    FMODE_READ | FMODE_EXCL, &holder);
1528 	if (!IS_ERR(hib_resume_bdev)) {
1529 		set_blocksize(hib_resume_bdev, PAGE_SIZE);
1530 		clear_page(swsusp_header);
1531 		error = hib_submit_io(REQ_OP_READ, 0,
1532 					swsusp_resume_block,
1533 					swsusp_header, NULL);
1534 		if (error)
1535 			goto put;
1536 
1537 		if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
1538 			memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
1539 			/* Reset swap signature now */
1540 			error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
1541 						swsusp_resume_block,
1542 						swsusp_header, NULL);
1543 		} else {
1544 			error = -EINVAL;
1545 		}
1546 		if (!error && swsusp_header->flags & SF_HW_SIG &&
1547 		    swsusp_header->hw_sig != swsusp_hardware_signature) {
1548 			pr_info("Suspend image hardware signature mismatch (%08x now %08x); aborting resume.\n",
1549 				swsusp_header->hw_sig, swsusp_hardware_signature);
1550 			error = -EINVAL;
1551 		}
1552 
1553 put:
1554 		if (error)
1555 			blkdev_put(hib_resume_bdev, FMODE_READ | FMODE_EXCL);
1556 		else
1557 			pr_debug("Image signature found, resuming\n");
1558 	} else {
1559 		error = PTR_ERR(hib_resume_bdev);
1560 	}
1561 
1562 	if (error)
1563 		pr_debug("Image not found (code %d)\n", error);
1564 
1565 	return error;
1566 }
1567 
1568 /**
1569  *	swsusp_close - close swap device.
1570  */
1571 
1572 void swsusp_close(fmode_t mode)
1573 {
1574 	if (IS_ERR(hib_resume_bdev)) {
1575 		pr_debug("Image device not initialised\n");
1576 		return;
1577 	}
1578 
1579 	blkdev_put(hib_resume_bdev, mode);
1580 }
1581 
1582 /**
1583  *      swsusp_unmark - Unmark swsusp signature in the resume device
1584  */
1585 
1586 #ifdef CONFIG_SUSPEND
1587 int swsusp_unmark(void)
1588 {
1589 	int error;
1590 
1591 	hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
1592 		      swsusp_header, NULL);
1593 	if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1594 		memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1595 		error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
1596 					swsusp_resume_block,
1597 					swsusp_header, NULL);
1598 	} else {
1599 		pr_err("Cannot find swsusp signature!\n");
1600 		error = -ENODEV;
1601 	}
1602 
1603 	/*
1604 	 * We just returned from suspend, we don't need the image any more.
1605 	 */
1606 	free_all_swap_pages(root_swap);
1607 
1608 	return error;
1609 }
1610 #endif
1611 
1612 static int __init swsusp_header_init(void)
1613 {
1614 	swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1615 	if (!swsusp_header)
1616 		panic("Could not allocate memory for swsusp_header\n");
1617 	return 0;
1618 }
1619 
1620 core_initcall(swsusp_header_init);
1621