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