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