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