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