1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/kernel/power/swap.c
5 * This file provides functions for reading the suspend image from
6 * and writing it to a swap partition.
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>
13 #define pr_fmt(fmt) "PM: " fmt
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>
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>
36 #define HIBERNATE_SIG "S1SUSPEND"
38 u32 swsusp_hardware_signature;
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.
45 static bool clean_pages_on_read;
46 static bool clean_pages_on_decompress;
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.
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.
59 * During resume we pick up all swap_map_page structures into a list.
62 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
65 * Number of free pages that are not high.
67 static inline unsigned long low_free_pages(void)
69 return nr_free_pages() - nr_free_highpages();
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.
76 static inline unsigned long reqd_free_pages(void)
78 return low_free_pages() / 2;
81 struct swap_map_page {
82 sector_t entries[MAP_PAGE_ENTRIES];
86 struct swap_map_page_list {
87 struct swap_map_page *map;
88 struct swap_map_page_list *next;
92 * The swap_map_handle structure is used for handling swap in
96 struct swap_map_handle {
97 struct swap_map_page *cur;
98 struct swap_map_page_list *maps;
100 sector_t first_sector;
102 unsigned long reqd_free_pages;
106 struct swsusp_header {
107 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
108 sizeof(u32) - sizeof(u32)];
112 unsigned int flags; /* Flags to pass to the "boot" kernel */
117 static struct swsusp_header *swsusp_header;
120 * The following functions are used for tracing the allocated
121 * swap pages, so that they can be freed in case of an error.
124 struct swsusp_extent {
130 static struct rb_root swsusp_extents = RB_ROOT;
132 static int swsusp_extents_insert(unsigned long swap_offset)
134 struct rb_node **new = &(swsusp_extents.rb_node);
135 struct rb_node *parent = NULL;
136 struct swsusp_extent *ext;
138 /* Figure out where to put the new node */
140 ext = rb_entry(*new, struct swsusp_extent, node);
142 if (swap_offset < ext->start) {
144 if (swap_offset == ext->start - 1) {
148 new = &((*new)->rb_left);
149 } else if (swap_offset > ext->end) {
151 if (swap_offset == ext->end + 1) {
155 new = &((*new)->rb_right);
157 /* It already is in the tree */
161 /* Add the new node and rebalance the tree. */
162 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
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);
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.
178 sector_t alloc_swapdev_block(int swap)
180 unsigned long offset;
182 offset = swp_offset(get_swap_page_of_type(swap));
184 if (swsusp_extents_insert(offset))
185 swap_free(swp_entry(swap, offset));
187 return swapdev_block(swap, offset);
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
198 void free_all_swap_pages(int swap)
200 struct rb_node *node;
202 while ((node = swsusp_extents.rb_node)) {
203 struct swsusp_extent *ext;
204 unsigned long offset;
206 ext = rb_entry(node, struct swsusp_extent, node);
207 rb_erase(node, &swsusp_extents);
208 for (offset = ext->start; offset <= ext->end; offset++)
209 swap_free(swp_entry(swap, offset));
215 int swsusp_swap_in_use(void)
217 return (swsusp_extents.rb_node != NULL);
224 static unsigned short root_swap = 0xffff;
225 static struct block_device *hib_resume_bdev;
227 struct hib_bio_batch {
229 wait_queue_head_t wait;
231 struct blk_plug plug;
234 static void hib_init_batch(struct hib_bio_batch *hb)
236 atomic_set(&hb->count, 0);
237 init_waitqueue_head(&hb->wait);
238 hb->error = BLK_STS_OK;
239 blk_start_plug(&hb->plug);
242 static void hib_finish_batch(struct hib_bio_batch *hb)
244 blk_finish_plug(&hb->plug);
247 static void hib_end_io(struct bio *bio)
249 struct hib_bio_batch *hb = bio->bi_private;
250 struct page *page = bio_first_page_all(bio);
252 if (bio->bi_status) {
253 pr_alert("Read-error on swap-device (%u:%u:%Lu)\n",
254 MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
255 (unsigned long long)bio->bi_iter.bi_sector);
258 if (bio_data_dir(bio) == WRITE)
260 else if (clean_pages_on_read)
261 flush_icache_range((unsigned long)page_address(page),
262 (unsigned long)page_address(page) + PAGE_SIZE);
264 if (bio->bi_status && !hb->error)
265 hb->error = bio->bi_status;
266 if (atomic_dec_and_test(&hb->count))
272 static int hib_submit_io(int op, int op_flags, pgoff_t page_off, void *addr,
273 struct hib_bio_batch *hb)
275 struct page *page = virt_to_page(addr);
279 bio = bio_alloc(hib_resume_bdev, 1, op | op_flags,
280 GFP_NOIO | __GFP_HIGH);
281 bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
283 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
284 pr_err("Adding page to bio failed at %llu\n",
285 (unsigned long long)bio->bi_iter.bi_sector);
291 bio->bi_end_io = hib_end_io;
292 bio->bi_private = hb;
293 atomic_inc(&hb->count);
296 error = submit_bio_wait(bio);
303 static int hib_wait_io(struct hib_bio_batch *hb)
306 * We are relying on the behavior of blk_plug that a thread with
307 * a plug will flush the plug list before sleeping.
309 wait_event(hb->wait, atomic_read(&hb->count) == 0);
310 return blk_status_to_errno(hb->error);
316 static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
320 hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
321 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;
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);
337 pr_err("Swap header not found!\n");
344 * swsusp_swap_check - check if the resume device is a swap device
345 * and get its index (if so)
347 * This is called before saving image
349 static int swsusp_swap_check(void)
353 if (swsusp_resume_device)
354 res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
356 res = find_first_swap(&swsusp_resume_device);
361 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device, FMODE_WRITE,
363 if (IS_ERR(hib_resume_bdev))
364 return PTR_ERR(hib_resume_bdev);
366 res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
368 blkdev_put(hib_resume_bdev, FMODE_WRITE);
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
380 static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
389 src = (void *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
394 ret = hib_wait_io(hb); /* Free pages */
397 src = (void *)__get_free_page(GFP_NOIO |
404 hb = NULL; /* Go synchronous */
411 return hib_submit_io(REQ_OP_WRITE, REQ_SYNC, offset, src, hb);
414 static void release_swap_writer(struct swap_map_handle *handle)
417 free_page((unsigned long)handle->cur);
421 static int get_swap_writer(struct swap_map_handle *handle)
425 ret = swsusp_swap_check();
428 pr_err("Cannot find swap device, try swapon -a\n");
431 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
436 handle->cur_swap = alloc_swapdev_block(root_swap);
437 if (!handle->cur_swap) {
442 handle->reqd_free_pages = reqd_free_pages();
443 handle->first_sector = handle->cur_swap;
446 release_swap_writer(handle);
448 swsusp_close(FMODE_WRITE);
452 static int swap_write_page(struct swap_map_handle *handle, void *buf,
453 struct hib_bio_batch *hb)
460 offset = alloc_swapdev_block(root_swap);
461 error = write_page(buf, offset, hb);
464 handle->cur->entries[handle->k++] = offset;
465 if (handle->k >= MAP_PAGE_ENTRIES) {
466 offset = alloc_swapdev_block(root_swap);
469 handle->cur->next_swap = offset;
470 error = write_page(handle->cur, handle->cur_swap, hb);
473 clear_page(handle->cur);
474 handle->cur_swap = offset;
477 if (hb && low_free_pages() <= handle->reqd_free_pages) {
478 error = hib_wait_io(hb);
482 * Recalculate the number of required free pages, to
483 * make sure we never take more than half.
485 handle->reqd_free_pages = reqd_free_pages();
492 static int flush_swap_writer(struct swap_map_handle *handle)
494 if (handle->cur && handle->cur_swap)
495 return write_page(handle->cur, handle->cur_swap, NULL);
500 static int swap_writer_finish(struct swap_map_handle *handle,
501 unsigned int flags, int error)
505 error = mark_swapfiles(handle, flags);
507 flush_swap_writer(handle);
511 free_all_swap_pages(root_swap);
512 release_swap_writer(handle);
513 swsusp_close(FMODE_WRITE);
518 /* We need to remember how much compressed data we need to read. */
519 #define LZO_HEADER sizeof(size_t)
521 /* Number of pages/bytes we'll compress at one time. */
522 #define LZO_UNC_PAGES 32
523 #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
525 /* Number of pages/bytes we need for compressed data (worst case). */
526 #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
527 LZO_HEADER, PAGE_SIZE)
528 #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
530 /* Maximum number of threads for compression/decompression. */
531 #define LZO_THREADS 3
533 /* Minimum/maximum number of pages for read buffering. */
534 #define LZO_MIN_RD_PAGES 1024
535 #define LZO_MAX_RD_PAGES 8192
539 * save_image - save the suspend image data
542 static int save_image(struct swap_map_handle *handle,
543 struct snapshot_handle *snapshot,
544 unsigned int nr_to_write)
550 struct hib_bio_batch hb;
556 pr_info("Saving image data pages (%u pages)...\n",
558 m = nr_to_write / 10;
564 ret = snapshot_read_next(snapshot);
567 ret = swap_write_page(handle, data_of(*snapshot), &hb);
571 pr_info("Image saving progress: %3d%%\n",
575 err2 = hib_wait_io(&hb);
576 hib_finish_batch(&hb);
581 pr_info("Image saving done\n");
582 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
587 * Structure used for CRC32.
590 struct task_struct *thr; /* thread */
591 atomic_t ready; /* ready to start flag */
592 atomic_t stop; /* ready to stop flag */
593 unsigned run_threads; /* nr current threads */
594 wait_queue_head_t go; /* start crc update */
595 wait_queue_head_t done; /* crc update done */
596 u32 *crc32; /* points to handle's crc32 */
597 size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */
598 unsigned char *unc[LZO_THREADS]; /* uncompressed data */
602 * CRC32 update function that runs in its own thread.
604 static int crc32_threadfn(void *data)
606 struct crc_data *d = data;
610 wait_event(d->go, atomic_read(&d->ready) ||
611 kthread_should_stop());
612 if (kthread_should_stop()) {
614 atomic_set(&d->stop, 1);
618 atomic_set(&d->ready, 0);
620 for (i = 0; i < d->run_threads; i++)
621 *d->crc32 = crc32_le(*d->crc32,
622 d->unc[i], *d->unc_len[i]);
623 atomic_set(&d->stop, 1);
629 * Structure used for LZO data compression.
632 struct task_struct *thr; /* thread */
633 atomic_t ready; /* ready to start flag */
634 atomic_t stop; /* ready to stop flag */
635 int ret; /* return code */
636 wait_queue_head_t go; /* start compression */
637 wait_queue_head_t done; /* compression done */
638 size_t unc_len; /* uncompressed length */
639 size_t cmp_len; /* compressed length */
640 unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
641 unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
642 unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */
646 * Compression function that runs in its own thread.
648 static int lzo_compress_threadfn(void *data)
650 struct cmp_data *d = data;
653 wait_event(d->go, atomic_read(&d->ready) ||
654 kthread_should_stop());
655 if (kthread_should_stop()) {
658 atomic_set(&d->stop, 1);
662 atomic_set(&d->ready, 0);
664 d->ret = lzo1x_1_compress(d->unc, d->unc_len,
665 d->cmp + LZO_HEADER, &d->cmp_len,
667 atomic_set(&d->stop, 1);
674 * save_image_lzo - Save the suspend image data compressed with LZO.
675 * @handle: Swap map handle to use for saving the image.
676 * @snapshot: Image to read data from.
677 * @nr_to_write: Number of pages to save.
679 static int save_image_lzo(struct swap_map_handle *handle,
680 struct snapshot_handle *snapshot,
681 unsigned int nr_to_write)
687 struct hib_bio_batch hb;
691 unsigned thr, run_threads, nr_threads;
692 unsigned char *page = NULL;
693 struct cmp_data *data = NULL;
694 struct crc_data *crc = NULL;
699 * We'll limit the number of threads for compression to limit memory
702 nr_threads = num_online_cpus() - 1;
703 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
705 page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
707 pr_err("Failed to allocate LZO page\n");
712 data = vzalloc(array_size(nr_threads, sizeof(*data)));
714 pr_err("Failed to allocate LZO data\n");
719 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
721 pr_err("Failed to allocate crc\n");
727 * Start the compression threads.
729 for (thr = 0; thr < nr_threads; thr++) {
730 init_waitqueue_head(&data[thr].go);
731 init_waitqueue_head(&data[thr].done);
733 data[thr].thr = kthread_run(lzo_compress_threadfn,
735 "image_compress/%u", thr);
736 if (IS_ERR(data[thr].thr)) {
737 data[thr].thr = NULL;
738 pr_err("Cannot start compression threads\n");
745 * Start the CRC32 thread.
747 init_waitqueue_head(&crc->go);
748 init_waitqueue_head(&crc->done);
751 crc->crc32 = &handle->crc32;
752 for (thr = 0; thr < nr_threads; thr++) {
753 crc->unc[thr] = data[thr].unc;
754 crc->unc_len[thr] = &data[thr].unc_len;
757 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
758 if (IS_ERR(crc->thr)) {
760 pr_err("Cannot start CRC32 thread\n");
766 * Adjust the number of required free pages after all allocations have
767 * been done. We don't want to run out of pages when writing.
769 handle->reqd_free_pages = reqd_free_pages();
771 pr_info("Using %u thread(s) for compression\n", nr_threads);
772 pr_info("Compressing and saving image data (%u pages)...\n",
774 m = nr_to_write / 10;
780 for (thr = 0; thr < nr_threads; thr++) {
781 for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
782 ret = snapshot_read_next(snapshot);
789 memcpy(data[thr].unc + off,
790 data_of(*snapshot), PAGE_SIZE);
793 pr_info("Image saving progress: %3d%%\n",
800 data[thr].unc_len = off;
802 atomic_set(&data[thr].ready, 1);
803 wake_up(&data[thr].go);
809 crc->run_threads = thr;
810 atomic_set(&crc->ready, 1);
813 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
814 wait_event(data[thr].done,
815 atomic_read(&data[thr].stop));
816 atomic_set(&data[thr].stop, 0);
821 pr_err("LZO compression failed\n");
825 if (unlikely(!data[thr].cmp_len ||
827 lzo1x_worst_compress(data[thr].unc_len))) {
828 pr_err("Invalid LZO compressed length\n");
833 *(size_t *)data[thr].cmp = data[thr].cmp_len;
836 * Given we are writing one page at a time to disk, we
837 * copy that much from the buffer, although the last
838 * bit will likely be smaller than full page. This is
839 * OK - we saved the length of the compressed data, so
840 * any garbage at the end will be discarded when we
844 off < LZO_HEADER + data[thr].cmp_len;
846 memcpy(page, data[thr].cmp + off, PAGE_SIZE);
848 ret = swap_write_page(handle, page, &hb);
854 wait_event(crc->done, atomic_read(&crc->stop));
855 atomic_set(&crc->stop, 0);
859 err2 = hib_wait_io(&hb);
864 pr_info("Image saving done\n");
865 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
867 hib_finish_batch(&hb);
870 kthread_stop(crc->thr);
874 for (thr = 0; thr < nr_threads; thr++)
876 kthread_stop(data[thr].thr);
879 if (page) free_page((unsigned long)page);
885 * enough_swap - Make sure we have enough swap to save the image.
887 * Returns TRUE or FALSE after checking the total amount of swap
888 * space available from the resume partition.
891 static int enough_swap(unsigned int nr_pages)
893 unsigned int free_swap = count_swap_pages(root_swap, 1);
894 unsigned int required;
896 pr_debug("Free swap pages: %u\n", free_swap);
898 required = PAGES_FOR_IO + nr_pages;
899 return free_swap > required;
903 * swsusp_write - Write entire image and metadata.
904 * @flags: flags to pass to the "boot" kernel in the image header
906 * It is important _NOT_ to umount filesystems at this point. We want
907 * them synced (in case something goes wrong) but we DO not want to mark
908 * filesystem clean: it is not. (And it does not matter, if we resume
909 * correctly, we'll mark system clean, anyway.)
912 int swsusp_write(unsigned int flags)
914 struct swap_map_handle handle;
915 struct snapshot_handle snapshot;
916 struct swsusp_info *header;
920 pages = snapshot_get_image_size();
921 error = get_swap_writer(&handle);
923 pr_err("Cannot get swap writer\n");
926 if (flags & SF_NOCOMPRESS_MODE) {
927 if (!enough_swap(pages)) {
928 pr_err("Not enough free swap\n");
933 memset(&snapshot, 0, sizeof(struct snapshot_handle));
934 error = snapshot_read_next(&snapshot);
935 if (error < (int)PAGE_SIZE) {
941 header = (struct swsusp_info *)data_of(snapshot);
942 error = swap_write_page(&handle, header, NULL);
944 error = (flags & SF_NOCOMPRESS_MODE) ?
945 save_image(&handle, &snapshot, pages - 1) :
946 save_image_lzo(&handle, &snapshot, pages - 1);
949 error = swap_writer_finish(&handle, flags, error);
954 * The following functions allow us to read data using a swap map
955 * in a file-alike way
958 static void release_swap_reader(struct swap_map_handle *handle)
960 struct swap_map_page_list *tmp;
962 while (handle->maps) {
963 if (handle->maps->map)
964 free_page((unsigned long)handle->maps->map);
966 handle->maps = handle->maps->next;
972 static int get_swap_reader(struct swap_map_handle *handle,
973 unsigned int *flags_p)
976 struct swap_map_page_list *tmp, *last;
979 *flags_p = swsusp_header->flags;
981 if (!swsusp_header->image) /* how can this happen? */
985 last = handle->maps = NULL;
986 offset = swsusp_header->image;
988 tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
990 release_swap_reader(handle);
999 tmp->map = (struct swap_map_page *)
1000 __get_free_page(GFP_NOIO | __GFP_HIGH);
1002 release_swap_reader(handle);
1006 error = hib_submit_io(REQ_OP_READ, 0, offset, tmp->map, NULL);
1008 release_swap_reader(handle);
1011 offset = tmp->map->next_swap;
1014 handle->cur = handle->maps->map;
1018 static int swap_read_page(struct swap_map_handle *handle, void *buf,
1019 struct hib_bio_batch *hb)
1023 struct swap_map_page_list *tmp;
1027 offset = handle->cur->entries[handle->k];
1030 error = hib_submit_io(REQ_OP_READ, 0, offset, buf, hb);
1033 if (++handle->k >= MAP_PAGE_ENTRIES) {
1035 free_page((unsigned long)handle->maps->map);
1037 handle->maps = handle->maps->next;
1040 release_swap_reader(handle);
1042 handle->cur = handle->maps->map;
1047 static int swap_reader_finish(struct swap_map_handle *handle)
1049 release_swap_reader(handle);
1055 * load_image - load the image using the swap map handle
1056 * @handle and the snapshot handle @snapshot
1057 * (assume there are @nr_pages pages to load)
1060 static int load_image(struct swap_map_handle *handle,
1061 struct snapshot_handle *snapshot,
1062 unsigned int nr_to_read)
1068 struct hib_bio_batch hb;
1072 hib_init_batch(&hb);
1074 clean_pages_on_read = true;
1075 pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
1076 m = nr_to_read / 10;
1080 start = ktime_get();
1082 ret = snapshot_write_next(snapshot);
1085 ret = swap_read_page(handle, data_of(*snapshot), &hb);
1088 if (snapshot->sync_read)
1089 ret = hib_wait_io(&hb);
1092 if (!(nr_pages % m))
1093 pr_info("Image loading progress: %3d%%\n",
1097 err2 = hib_wait_io(&hb);
1098 hib_finish_batch(&hb);
1103 pr_info("Image loading done\n");
1104 snapshot_write_finalize(snapshot);
1105 if (!snapshot_image_loaded(snapshot))
1108 swsusp_show_speed(start, stop, nr_to_read, "Read");
1113 * Structure used for LZO data decompression.
1116 struct task_struct *thr; /* thread */
1117 atomic_t ready; /* ready to start flag */
1118 atomic_t stop; /* ready to stop flag */
1119 int ret; /* return code */
1120 wait_queue_head_t go; /* start decompression */
1121 wait_queue_head_t done; /* decompression done */
1122 size_t unc_len; /* uncompressed length */
1123 size_t cmp_len; /* compressed length */
1124 unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
1125 unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
1129 * Decompression function that runs in its own thread.
1131 static int lzo_decompress_threadfn(void *data)
1133 struct dec_data *d = data;
1136 wait_event(d->go, atomic_read(&d->ready) ||
1137 kthread_should_stop());
1138 if (kthread_should_stop()) {
1141 atomic_set(&d->stop, 1);
1145 atomic_set(&d->ready, 0);
1147 d->unc_len = LZO_UNC_SIZE;
1148 d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
1149 d->unc, &d->unc_len);
1150 if (clean_pages_on_decompress)
1151 flush_icache_range((unsigned long)d->unc,
1152 (unsigned long)d->unc + d->unc_len);
1154 atomic_set(&d->stop, 1);
1161 * load_image_lzo - Load compressed image data and decompress them with LZO.
1162 * @handle: Swap map handle to use for loading data.
1163 * @snapshot: Image to copy uncompressed data into.
1164 * @nr_to_read: Number of pages to load.
1166 static int load_image_lzo(struct swap_map_handle *handle,
1167 struct snapshot_handle *snapshot,
1168 unsigned int nr_to_read)
1173 struct hib_bio_batch hb;
1178 unsigned i, thr, run_threads, nr_threads;
1179 unsigned ring = 0, pg = 0, ring_size = 0,
1180 have = 0, want, need, asked = 0;
1181 unsigned long read_pages = 0;
1182 unsigned char **page = NULL;
1183 struct dec_data *data = NULL;
1184 struct crc_data *crc = NULL;
1186 hib_init_batch(&hb);
1189 * We'll limit the number of threads for decompression to limit memory
1192 nr_threads = num_online_cpus() - 1;
1193 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
1195 page = vmalloc(array_size(LZO_MAX_RD_PAGES, sizeof(*page)));
1197 pr_err("Failed to allocate LZO page\n");
1202 data = vzalloc(array_size(nr_threads, sizeof(*data)));
1204 pr_err("Failed to allocate LZO data\n");
1209 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
1211 pr_err("Failed to allocate crc\n");
1216 clean_pages_on_decompress = true;
1219 * Start the decompression threads.
1221 for (thr = 0; thr < nr_threads; thr++) {
1222 init_waitqueue_head(&data[thr].go);
1223 init_waitqueue_head(&data[thr].done);
1225 data[thr].thr = kthread_run(lzo_decompress_threadfn,
1227 "image_decompress/%u", thr);
1228 if (IS_ERR(data[thr].thr)) {
1229 data[thr].thr = NULL;
1230 pr_err("Cannot start decompression threads\n");
1237 * Start the CRC32 thread.
1239 init_waitqueue_head(&crc->go);
1240 init_waitqueue_head(&crc->done);
1243 crc->crc32 = &handle->crc32;
1244 for (thr = 0; thr < nr_threads; thr++) {
1245 crc->unc[thr] = data[thr].unc;
1246 crc->unc_len[thr] = &data[thr].unc_len;
1249 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1250 if (IS_ERR(crc->thr)) {
1252 pr_err("Cannot start CRC32 thread\n");
1258 * Set the number of pages for read buffering.
1259 * This is complete guesswork, because we'll only know the real
1260 * picture once prepare_image() is called, which is much later on
1261 * during the image load phase. We'll assume the worst case and
1262 * say that none of the image pages are from high memory.
1264 if (low_free_pages() > snapshot_get_image_size())
1265 read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1266 read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES);
1268 for (i = 0; i < read_pages; i++) {
1269 page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
1270 GFP_NOIO | __GFP_HIGH :
1271 GFP_NOIO | __GFP_NOWARN |
1275 if (i < LZO_CMP_PAGES) {
1277 pr_err("Failed to allocate LZO pages\n");
1285 want = ring_size = i;
1287 pr_info("Using %u thread(s) for decompression\n", nr_threads);
1288 pr_info("Loading and decompressing image data (%u pages)...\n",
1290 m = nr_to_read / 10;
1294 start = ktime_get();
1296 ret = snapshot_write_next(snapshot);
1301 for (i = 0; !eof && i < want; i++) {
1302 ret = swap_read_page(handle, page[ring], &hb);
1305 * On real read error, finish. On end of data,
1306 * set EOF flag and just exit the read loop.
1309 handle->cur->entries[handle->k]) {
1316 if (++ring >= ring_size)
1323 * We are out of data, wait for some more.
1329 ret = hib_wait_io(&hb);
1338 if (crc->run_threads) {
1339 wait_event(crc->done, atomic_read(&crc->stop));
1340 atomic_set(&crc->stop, 0);
1341 crc->run_threads = 0;
1344 for (thr = 0; have && thr < nr_threads; thr++) {
1345 data[thr].cmp_len = *(size_t *)page[pg];
1346 if (unlikely(!data[thr].cmp_len ||
1348 lzo1x_worst_compress(LZO_UNC_SIZE))) {
1349 pr_err("Invalid LZO compressed length\n");
1354 need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
1365 off < LZO_HEADER + data[thr].cmp_len;
1367 memcpy(data[thr].cmp + off,
1368 page[pg], PAGE_SIZE);
1371 if (++pg >= ring_size)
1375 atomic_set(&data[thr].ready, 1);
1376 wake_up(&data[thr].go);
1380 * Wait for more data while we are decompressing.
1382 if (have < LZO_CMP_PAGES && asked) {
1383 ret = hib_wait_io(&hb);
1392 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1393 wait_event(data[thr].done,
1394 atomic_read(&data[thr].stop));
1395 atomic_set(&data[thr].stop, 0);
1397 ret = data[thr].ret;
1400 pr_err("LZO decompression failed\n");
1404 if (unlikely(!data[thr].unc_len ||
1405 data[thr].unc_len > LZO_UNC_SIZE ||
1406 data[thr].unc_len & (PAGE_SIZE - 1))) {
1407 pr_err("Invalid LZO uncompressed length\n");
1413 off < data[thr].unc_len; off += PAGE_SIZE) {
1414 memcpy(data_of(*snapshot),
1415 data[thr].unc + off, PAGE_SIZE);
1417 if (!(nr_pages % m))
1418 pr_info("Image loading progress: %3d%%\n",
1422 ret = snapshot_write_next(snapshot);
1424 crc->run_threads = thr + 1;
1425 atomic_set(&crc->ready, 1);
1432 crc->run_threads = thr;
1433 atomic_set(&crc->ready, 1);
1438 if (crc->run_threads) {
1439 wait_event(crc->done, atomic_read(&crc->stop));
1440 atomic_set(&crc->stop, 0);
1444 pr_info("Image loading done\n");
1445 snapshot_write_finalize(snapshot);
1446 if (!snapshot_image_loaded(snapshot))
1449 if (swsusp_header->flags & SF_CRC32_MODE) {
1450 if(handle->crc32 != swsusp_header->crc32) {
1451 pr_err("Invalid image CRC32!\n");
1457 swsusp_show_speed(start, stop, nr_to_read, "Read");
1459 hib_finish_batch(&hb);
1460 for (i = 0; i < ring_size; i++)
1461 free_page((unsigned long)page[i]);
1464 kthread_stop(crc->thr);
1468 for (thr = 0; thr < nr_threads; thr++)
1470 kthread_stop(data[thr].thr);
1479 * swsusp_read - read the hibernation image.
1480 * @flags_p: flags passed by the "frozen" kernel in the image header should
1481 * be written into this memory location
1484 int swsusp_read(unsigned int *flags_p)
1487 struct swap_map_handle handle;
1488 struct snapshot_handle snapshot;
1489 struct swsusp_info *header;
1491 memset(&snapshot, 0, sizeof(struct snapshot_handle));
1492 error = snapshot_write_next(&snapshot);
1493 if (error < (int)PAGE_SIZE)
1494 return error < 0 ? error : -EFAULT;
1495 header = (struct swsusp_info *)data_of(snapshot);
1496 error = get_swap_reader(&handle, flags_p);
1500 error = swap_read_page(&handle, header, NULL);
1502 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1503 load_image(&handle, &snapshot, header->pages - 1) :
1504 load_image_lzo(&handle, &snapshot, header->pages - 1);
1506 swap_reader_finish(&handle);
1509 pr_debug("Image successfully loaded\n");
1511 pr_debug("Error %d resuming\n", error);
1516 * swsusp_check - Check for swsusp signature in the resume device
1519 int swsusp_check(void)
1524 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
1525 FMODE_READ | FMODE_EXCL, &holder);
1526 if (!IS_ERR(hib_resume_bdev)) {
1527 set_blocksize(hib_resume_bdev, PAGE_SIZE);
1528 clear_page(swsusp_header);
1529 error = hib_submit_io(REQ_OP_READ, 0,
1530 swsusp_resume_block,
1531 swsusp_header, NULL);
1535 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
1536 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
1537 /* Reset swap signature now */
1538 error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
1539 swsusp_resume_block,
1540 swsusp_header, NULL);
1544 if (!error && swsusp_header->flags & SF_HW_SIG &&
1545 swsusp_header->hw_sig != swsusp_hardware_signature) {
1546 pr_info("Suspend image hardware signature mismatch (%08x now %08x); aborting resume.\n",
1547 swsusp_header->hw_sig, swsusp_hardware_signature);
1553 blkdev_put(hib_resume_bdev, FMODE_READ | FMODE_EXCL);
1555 pr_debug("Image signature found, resuming\n");
1557 error = PTR_ERR(hib_resume_bdev);
1561 pr_debug("Image not found (code %d)\n", error);
1567 * swsusp_close - close swap device.
1570 void swsusp_close(fmode_t mode)
1572 if (IS_ERR(hib_resume_bdev)) {
1573 pr_debug("Image device not initialised\n");
1577 blkdev_put(hib_resume_bdev, mode);
1581 * swsusp_unmark - Unmark swsusp signature in the resume device
1584 #ifdef CONFIG_SUSPEND
1585 int swsusp_unmark(void)
1589 hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
1590 swsusp_header, NULL);
1591 if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1592 memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1593 error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
1594 swsusp_resume_block,
1595 swsusp_header, NULL);
1597 pr_err("Cannot find swsusp signature!\n");
1602 * We just returned from suspend, we don't need the image any more.
1604 free_all_swap_pages(root_swap);
1610 static int __init swsusp_header_init(void)
1612 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1614 panic("Could not allocate memory for swsusp_header\n");
1618 core_initcall(swsusp_header_init);