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/genhd.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>
26 #include <linux/slab.h>
27 #include <linux/lzo.h>
28 #include <linux/vmalloc.h>
29 #include <linux/cpumask.h>
30 #include <linux/atomic.h>
31 #include <linux/kthread.h>
32 #include <linux/crc32.h>
33 #include <linux/ktime.h>
37 #define HIBERNATE_SIG "S1SUSPEND"
39 u32 swsusp_hardware_signature;
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.
46 static bool clean_pages_on_read;
47 static bool clean_pages_on_decompress;
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.
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.
60 * During resume we pick up all swap_map_page structures into a list.
63 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
66 * Number of free pages that are not high.
68 static inline unsigned long low_free_pages(void)
70 return nr_free_pages() - nr_free_highpages();
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.
77 static inline unsigned long reqd_free_pages(void)
79 return low_free_pages() / 2;
82 struct swap_map_page {
83 sector_t entries[MAP_PAGE_ENTRIES];
87 struct swap_map_page_list {
88 struct swap_map_page *map;
89 struct swap_map_page_list *next;
93 * The swap_map_handle structure is used for handling swap in
97 struct swap_map_handle {
98 struct swap_map_page *cur;
99 struct swap_map_page_list *maps;
101 sector_t first_sector;
103 unsigned long reqd_free_pages;
107 struct swsusp_header {
108 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
109 sizeof(u32) - sizeof(u32)];
113 unsigned int flags; /* Flags to pass to the "boot" kernel */
118 static struct swsusp_header *swsusp_header;
121 * The following functions are used for tracing the allocated
122 * swap pages, so that they can be freed in case of an error.
125 struct swsusp_extent {
131 static struct rb_root swsusp_extents = RB_ROOT;
133 static int swsusp_extents_insert(unsigned long swap_offset)
135 struct rb_node **new = &(swsusp_extents.rb_node);
136 struct rb_node *parent = NULL;
137 struct swsusp_extent *ext;
139 /* Figure out where to put the new node */
141 ext = rb_entry(*new, struct swsusp_extent, node);
143 if (swap_offset < ext->start) {
145 if (swap_offset == ext->start - 1) {
149 new = &((*new)->rb_left);
150 } else if (swap_offset > ext->end) {
152 if (swap_offset == ext->end + 1) {
156 new = &((*new)->rb_right);
158 /* It already is in the tree */
162 /* Add the new node and rebalance the tree. */
163 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
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);
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.
179 sector_t alloc_swapdev_block(int swap)
181 unsigned long offset;
183 offset = swp_offset(get_swap_page_of_type(swap));
185 if (swsusp_extents_insert(offset))
186 swap_free(swp_entry(swap, offset));
188 return swapdev_block(swap, offset);
194 * free_all_swap_pages - free swap pages allocated for saving image data.
195 * It also frees the extents used to register which swap entries had been
199 void free_all_swap_pages(int swap)
201 struct rb_node *node;
203 while ((node = swsusp_extents.rb_node)) {
204 struct swsusp_extent *ext;
205 unsigned long offset;
207 ext = rb_entry(node, struct swsusp_extent, node);
208 rb_erase(node, &swsusp_extents);
209 for (offset = ext->start; offset <= ext->end; offset++)
210 swap_free(swp_entry(swap, offset));
216 int swsusp_swap_in_use(void)
218 return (swsusp_extents.rb_node != NULL);
225 static unsigned short root_swap = 0xffff;
226 static struct block_device *hib_resume_bdev;
228 struct hib_bio_batch {
230 wait_queue_head_t wait;
232 struct blk_plug plug;
235 static void hib_init_batch(struct hib_bio_batch *hb)
237 atomic_set(&hb->count, 0);
238 init_waitqueue_head(&hb->wait);
239 hb->error = BLK_STS_OK;
240 blk_start_plug(&hb->plug);
243 static void hib_finish_batch(struct hib_bio_batch *hb)
245 blk_finish_plug(&hb->plug);
248 static void hib_end_io(struct bio *bio)
250 struct hib_bio_batch *hb = bio->bi_private;
251 struct page *page = bio_first_page_all(bio);
253 if (bio->bi_status) {
254 pr_alert("Read-error on swap-device (%u:%u:%Lu)\n",
255 MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
256 (unsigned long long)bio->bi_iter.bi_sector);
259 if (bio_data_dir(bio) == WRITE)
261 else if (clean_pages_on_read)
262 flush_icache_range((unsigned long)page_address(page),
263 (unsigned long)page_address(page) + PAGE_SIZE);
265 if (bio->bi_status && !hb->error)
266 hb->error = bio->bi_status;
267 if (atomic_dec_and_test(&hb->count))
273 static int hib_submit_io(int op, int op_flags, pgoff_t page_off, void *addr,
274 struct hib_bio_batch *hb)
276 struct page *page = virt_to_page(addr);
280 bio = bio_alloc(GFP_NOIO | __GFP_HIGH, 1);
281 bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
282 bio_set_dev(bio, hib_resume_bdev);
283 bio_set_op_attrs(bio, op, op_flags);
285 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
286 pr_err("Adding page to bio failed at %llu\n",
287 (unsigned long long)bio->bi_iter.bi_sector);
293 bio->bi_end_io = hib_end_io;
294 bio->bi_private = hb;
295 atomic_inc(&hb->count);
298 error = submit_bio_wait(bio);
305 static int hib_wait_io(struct hib_bio_batch *hb)
308 * We are relying on the behavior of blk_plug that a thread with
309 * a plug will flush the plug list before sleeping.
311 wait_event(hb->wait, atomic_read(&hb->count) == 0);
312 return blk_status_to_errno(hb->error);
318 static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
322 hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
323 swsusp_header, NULL);
324 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
325 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
326 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
327 memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
328 swsusp_header->image = handle->first_sector;
329 if (swsusp_hardware_signature) {
330 swsusp_header->hw_sig = swsusp_hardware_signature;
333 swsusp_header->flags = flags;
334 if (flags & SF_CRC32_MODE)
335 swsusp_header->crc32 = handle->crc32;
336 error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
337 swsusp_resume_block, swsusp_header, NULL);
339 pr_err("Swap header not found!\n");
346 * swsusp_swap_check - check if the resume device is a swap device
347 * and get its index (if so)
349 * This is called before saving image
351 static int swsusp_swap_check(void)
355 if (swsusp_resume_device)
356 res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
358 res = find_first_swap(&swsusp_resume_device);
363 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device, FMODE_WRITE,
365 if (IS_ERR(hib_resume_bdev))
366 return PTR_ERR(hib_resume_bdev);
368 res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
370 blkdev_put(hib_resume_bdev, FMODE_WRITE);
376 * write_page - Write one page to given swap location.
377 * @buf: Address we're writing.
378 * @offset: Offset of the swap page we're writing to.
379 * @hb: bio completion batch
382 static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
391 src = (void *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
396 ret = hib_wait_io(hb); /* Free pages */
399 src = (void *)__get_free_page(GFP_NOIO |
406 hb = NULL; /* Go synchronous */
413 return hib_submit_io(REQ_OP_WRITE, REQ_SYNC, offset, src, hb);
416 static void release_swap_writer(struct swap_map_handle *handle)
419 free_page((unsigned long)handle->cur);
423 static int get_swap_writer(struct swap_map_handle *handle)
427 ret = swsusp_swap_check();
430 pr_err("Cannot find swap device, try swapon -a\n");
433 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
438 handle->cur_swap = alloc_swapdev_block(root_swap);
439 if (!handle->cur_swap) {
444 handle->reqd_free_pages = reqd_free_pages();
445 handle->first_sector = handle->cur_swap;
448 release_swap_writer(handle);
450 swsusp_close(FMODE_WRITE);
454 static int swap_write_page(struct swap_map_handle *handle, void *buf,
455 struct hib_bio_batch *hb)
462 offset = alloc_swapdev_block(root_swap);
463 error = write_page(buf, offset, hb);
466 handle->cur->entries[handle->k++] = offset;
467 if (handle->k >= MAP_PAGE_ENTRIES) {
468 offset = alloc_swapdev_block(root_swap);
471 handle->cur->next_swap = offset;
472 error = write_page(handle->cur, handle->cur_swap, hb);
475 clear_page(handle->cur);
476 handle->cur_swap = offset;
479 if (hb && low_free_pages() <= handle->reqd_free_pages) {
480 error = hib_wait_io(hb);
484 * Recalculate the number of required free pages, to
485 * make sure we never take more than half.
487 handle->reqd_free_pages = reqd_free_pages();
494 static int flush_swap_writer(struct swap_map_handle *handle)
496 if (handle->cur && handle->cur_swap)
497 return write_page(handle->cur, handle->cur_swap, NULL);
502 static int swap_writer_finish(struct swap_map_handle *handle,
503 unsigned int flags, int error)
507 error = mark_swapfiles(handle, flags);
509 flush_swap_writer(handle);
513 free_all_swap_pages(root_swap);
514 release_swap_writer(handle);
515 swsusp_close(FMODE_WRITE);
520 /* We need to remember how much compressed data we need to read. */
521 #define LZO_HEADER sizeof(size_t)
523 /* Number of pages/bytes we'll compress at one time. */
524 #define LZO_UNC_PAGES 32
525 #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
527 /* Number of pages/bytes we need for compressed data (worst case). */
528 #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
529 LZO_HEADER, PAGE_SIZE)
530 #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
532 /* Maximum number of threads for compression/decompression. */
533 #define LZO_THREADS 3
535 /* Minimum/maximum number of pages for read buffering. */
536 #define LZO_MIN_RD_PAGES 1024
537 #define LZO_MAX_RD_PAGES 8192
541 * save_image - save the suspend image data
544 static int save_image(struct swap_map_handle *handle,
545 struct snapshot_handle *snapshot,
546 unsigned int nr_to_write)
552 struct hib_bio_batch hb;
558 pr_info("Saving image data pages (%u pages)...\n",
560 m = nr_to_write / 10;
566 ret = snapshot_read_next(snapshot);
569 ret = swap_write_page(handle, data_of(*snapshot), &hb);
573 pr_info("Image saving progress: %3d%%\n",
577 err2 = hib_wait_io(&hb);
578 hib_finish_batch(&hb);
583 pr_info("Image saving done\n");
584 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
589 * Structure used for CRC32.
592 struct task_struct *thr; /* thread */
593 atomic_t ready; /* ready to start flag */
594 atomic_t stop; /* ready to stop flag */
595 unsigned run_threads; /* nr current threads */
596 wait_queue_head_t go; /* start crc update */
597 wait_queue_head_t done; /* crc update done */
598 u32 *crc32; /* points to handle's crc32 */
599 size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */
600 unsigned char *unc[LZO_THREADS]; /* uncompressed data */
604 * CRC32 update function that runs in its own thread.
606 static int crc32_threadfn(void *data)
608 struct crc_data *d = data;
612 wait_event(d->go, atomic_read(&d->ready) ||
613 kthread_should_stop());
614 if (kthread_should_stop()) {
616 atomic_set(&d->stop, 1);
620 atomic_set(&d->ready, 0);
622 for (i = 0; i < d->run_threads; i++)
623 *d->crc32 = crc32_le(*d->crc32,
624 d->unc[i], *d->unc_len[i]);
625 atomic_set(&d->stop, 1);
631 * Structure used for LZO data compression.
634 struct task_struct *thr; /* thread */
635 atomic_t ready; /* ready to start flag */
636 atomic_t stop; /* ready to stop flag */
637 int ret; /* return code */
638 wait_queue_head_t go; /* start compression */
639 wait_queue_head_t done; /* compression done */
640 size_t unc_len; /* uncompressed length */
641 size_t cmp_len; /* compressed length */
642 unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
643 unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
644 unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */
648 * Compression function that runs in its own thread.
650 static int lzo_compress_threadfn(void *data)
652 struct cmp_data *d = data;
655 wait_event(d->go, atomic_read(&d->ready) ||
656 kthread_should_stop());
657 if (kthread_should_stop()) {
660 atomic_set(&d->stop, 1);
664 atomic_set(&d->ready, 0);
666 d->ret = lzo1x_1_compress(d->unc, d->unc_len,
667 d->cmp + LZO_HEADER, &d->cmp_len,
669 atomic_set(&d->stop, 1);
676 * save_image_lzo - Save the suspend image data compressed with LZO.
677 * @handle: Swap map handle to use for saving the image.
678 * @snapshot: Image to read data from.
679 * @nr_to_write: Number of pages to save.
681 static int save_image_lzo(struct swap_map_handle *handle,
682 struct snapshot_handle *snapshot,
683 unsigned int nr_to_write)
689 struct hib_bio_batch hb;
693 unsigned thr, run_threads, nr_threads;
694 unsigned char *page = NULL;
695 struct cmp_data *data = NULL;
696 struct crc_data *crc = NULL;
701 * We'll limit the number of threads for compression to limit memory
704 nr_threads = num_online_cpus() - 1;
705 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
707 page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
709 pr_err("Failed to allocate LZO page\n");
714 data = vzalloc(array_size(nr_threads, sizeof(*data)));
716 pr_err("Failed to allocate LZO data\n");
721 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
723 pr_err("Failed to allocate crc\n");
729 * Start the compression threads.
731 for (thr = 0; thr < nr_threads; thr++) {
732 init_waitqueue_head(&data[thr].go);
733 init_waitqueue_head(&data[thr].done);
735 data[thr].thr = kthread_run(lzo_compress_threadfn,
737 "image_compress/%u", thr);
738 if (IS_ERR(data[thr].thr)) {
739 data[thr].thr = NULL;
740 pr_err("Cannot start compression threads\n");
747 * Start the CRC32 thread.
749 init_waitqueue_head(&crc->go);
750 init_waitqueue_head(&crc->done);
753 crc->crc32 = &handle->crc32;
754 for (thr = 0; thr < nr_threads; thr++) {
755 crc->unc[thr] = data[thr].unc;
756 crc->unc_len[thr] = &data[thr].unc_len;
759 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
760 if (IS_ERR(crc->thr)) {
762 pr_err("Cannot start CRC32 thread\n");
768 * Adjust the number of required free pages after all allocations have
769 * been done. We don't want to run out of pages when writing.
771 handle->reqd_free_pages = reqd_free_pages();
773 pr_info("Using %u thread(s) for compression\n", nr_threads);
774 pr_info("Compressing and saving image data (%u pages)...\n",
776 m = nr_to_write / 10;
782 for (thr = 0; thr < nr_threads; thr++) {
783 for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
784 ret = snapshot_read_next(snapshot);
791 memcpy(data[thr].unc + off,
792 data_of(*snapshot), PAGE_SIZE);
795 pr_info("Image saving progress: %3d%%\n",
802 data[thr].unc_len = off;
804 atomic_set(&data[thr].ready, 1);
805 wake_up(&data[thr].go);
811 crc->run_threads = thr;
812 atomic_set(&crc->ready, 1);
815 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
816 wait_event(data[thr].done,
817 atomic_read(&data[thr].stop));
818 atomic_set(&data[thr].stop, 0);
823 pr_err("LZO compression failed\n");
827 if (unlikely(!data[thr].cmp_len ||
829 lzo1x_worst_compress(data[thr].unc_len))) {
830 pr_err("Invalid LZO compressed length\n");
835 *(size_t *)data[thr].cmp = data[thr].cmp_len;
838 * Given we are writing one page at a time to disk, we
839 * copy that much from the buffer, although the last
840 * bit will likely be smaller than full page. This is
841 * OK - we saved the length of the compressed data, so
842 * any garbage at the end will be discarded when we
846 off < LZO_HEADER + data[thr].cmp_len;
848 memcpy(page, data[thr].cmp + off, PAGE_SIZE);
850 ret = swap_write_page(handle, page, &hb);
856 wait_event(crc->done, atomic_read(&crc->stop));
857 atomic_set(&crc->stop, 0);
861 err2 = hib_wait_io(&hb);
866 pr_info("Image saving done\n");
867 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
869 hib_finish_batch(&hb);
872 kthread_stop(crc->thr);
876 for (thr = 0; thr < nr_threads; thr++)
878 kthread_stop(data[thr].thr);
881 if (page) free_page((unsigned long)page);
887 * enough_swap - Make sure we have enough swap to save the image.
889 * Returns TRUE or FALSE after checking the total amount of swap
890 * space available from the resume partition.
893 static int enough_swap(unsigned int nr_pages)
895 unsigned int free_swap = count_swap_pages(root_swap, 1);
896 unsigned int required;
898 pr_debug("Free swap pages: %u\n", free_swap);
900 required = PAGES_FOR_IO + nr_pages;
901 return free_swap > required;
905 * swsusp_write - Write entire image and metadata.
906 * @flags: flags to pass to the "boot" kernel in the image header
908 * It is important _NOT_ to umount filesystems at this point. We want
909 * them synced (in case something goes wrong) but we DO not want to mark
910 * filesystem clean: it is not. (And it does not matter, if we resume
911 * correctly, we'll mark system clean, anyway.)
914 int swsusp_write(unsigned int flags)
916 struct swap_map_handle handle;
917 struct snapshot_handle snapshot;
918 struct swsusp_info *header;
922 pages = snapshot_get_image_size();
923 error = get_swap_writer(&handle);
925 pr_err("Cannot get swap writer\n");
928 if (flags & SF_NOCOMPRESS_MODE) {
929 if (!enough_swap(pages)) {
930 pr_err("Not enough free swap\n");
935 memset(&snapshot, 0, sizeof(struct snapshot_handle));
936 error = snapshot_read_next(&snapshot);
937 if (error < (int)PAGE_SIZE) {
943 header = (struct swsusp_info *)data_of(snapshot);
944 error = swap_write_page(&handle, header, NULL);
946 error = (flags & SF_NOCOMPRESS_MODE) ?
947 save_image(&handle, &snapshot, pages - 1) :
948 save_image_lzo(&handle, &snapshot, pages - 1);
951 error = swap_writer_finish(&handle, flags, error);
956 * The following functions allow us to read data using a swap map
957 * in a file-alike way
960 static void release_swap_reader(struct swap_map_handle *handle)
962 struct swap_map_page_list *tmp;
964 while (handle->maps) {
965 if (handle->maps->map)
966 free_page((unsigned long)handle->maps->map);
968 handle->maps = handle->maps->next;
974 static int get_swap_reader(struct swap_map_handle *handle,
975 unsigned int *flags_p)
978 struct swap_map_page_list *tmp, *last;
981 *flags_p = swsusp_header->flags;
983 if (!swsusp_header->image) /* how can this happen? */
987 last = handle->maps = NULL;
988 offset = swsusp_header->image;
990 tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
992 release_swap_reader(handle);
1001 tmp->map = (struct swap_map_page *)
1002 __get_free_page(GFP_NOIO | __GFP_HIGH);
1004 release_swap_reader(handle);
1008 error = hib_submit_io(REQ_OP_READ, 0, offset, tmp->map, NULL);
1010 release_swap_reader(handle);
1013 offset = tmp->map->next_swap;
1016 handle->cur = handle->maps->map;
1020 static int swap_read_page(struct swap_map_handle *handle, void *buf,
1021 struct hib_bio_batch *hb)
1025 struct swap_map_page_list *tmp;
1029 offset = handle->cur->entries[handle->k];
1032 error = hib_submit_io(REQ_OP_READ, 0, offset, buf, hb);
1035 if (++handle->k >= MAP_PAGE_ENTRIES) {
1037 free_page((unsigned long)handle->maps->map);
1039 handle->maps = handle->maps->next;
1042 release_swap_reader(handle);
1044 handle->cur = handle->maps->map;
1049 static int swap_reader_finish(struct swap_map_handle *handle)
1051 release_swap_reader(handle);
1057 * load_image - load the image using the swap map handle
1058 * @handle and the snapshot handle @snapshot
1059 * (assume there are @nr_pages pages to load)
1062 static int load_image(struct swap_map_handle *handle,
1063 struct snapshot_handle *snapshot,
1064 unsigned int nr_to_read)
1070 struct hib_bio_batch hb;
1074 hib_init_batch(&hb);
1076 clean_pages_on_read = true;
1077 pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
1078 m = nr_to_read / 10;
1082 start = ktime_get();
1084 ret = snapshot_write_next(snapshot);
1087 ret = swap_read_page(handle, data_of(*snapshot), &hb);
1090 if (snapshot->sync_read)
1091 ret = hib_wait_io(&hb);
1094 if (!(nr_pages % m))
1095 pr_info("Image loading progress: %3d%%\n",
1099 err2 = hib_wait_io(&hb);
1100 hib_finish_batch(&hb);
1105 pr_info("Image loading done\n");
1106 snapshot_write_finalize(snapshot);
1107 if (!snapshot_image_loaded(snapshot))
1110 swsusp_show_speed(start, stop, nr_to_read, "Read");
1115 * Structure used for LZO data decompression.
1118 struct task_struct *thr; /* thread */
1119 atomic_t ready; /* ready to start flag */
1120 atomic_t stop; /* ready to stop flag */
1121 int ret; /* return code */
1122 wait_queue_head_t go; /* start decompression */
1123 wait_queue_head_t done; /* decompression done */
1124 size_t unc_len; /* uncompressed length */
1125 size_t cmp_len; /* compressed length */
1126 unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
1127 unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
1131 * Decompression function that runs in its own thread.
1133 static int lzo_decompress_threadfn(void *data)
1135 struct dec_data *d = data;
1138 wait_event(d->go, atomic_read(&d->ready) ||
1139 kthread_should_stop());
1140 if (kthread_should_stop()) {
1143 atomic_set(&d->stop, 1);
1147 atomic_set(&d->ready, 0);
1149 d->unc_len = LZO_UNC_SIZE;
1150 d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
1151 d->unc, &d->unc_len);
1152 if (clean_pages_on_decompress)
1153 flush_icache_range((unsigned long)d->unc,
1154 (unsigned long)d->unc + d->unc_len);
1156 atomic_set(&d->stop, 1);
1163 * load_image_lzo - Load compressed image data and decompress them with LZO.
1164 * @handle: Swap map handle to use for loading data.
1165 * @snapshot: Image to copy uncompressed data into.
1166 * @nr_to_read: Number of pages to load.
1168 static int load_image_lzo(struct swap_map_handle *handle,
1169 struct snapshot_handle *snapshot,
1170 unsigned int nr_to_read)
1175 struct hib_bio_batch hb;
1180 unsigned i, thr, run_threads, nr_threads;
1181 unsigned ring = 0, pg = 0, ring_size = 0,
1182 have = 0, want, need, asked = 0;
1183 unsigned long read_pages = 0;
1184 unsigned char **page = NULL;
1185 struct dec_data *data = NULL;
1186 struct crc_data *crc = NULL;
1188 hib_init_batch(&hb);
1191 * We'll limit the number of threads for decompression to limit memory
1194 nr_threads = num_online_cpus() - 1;
1195 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
1197 page = vmalloc(array_size(LZO_MAX_RD_PAGES, sizeof(*page)));
1199 pr_err("Failed to allocate LZO page\n");
1204 data = vzalloc(array_size(nr_threads, sizeof(*data)));
1206 pr_err("Failed to allocate LZO data\n");
1211 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
1213 pr_err("Failed to allocate crc\n");
1218 clean_pages_on_decompress = true;
1221 * Start the decompression threads.
1223 for (thr = 0; thr < nr_threads; thr++) {
1224 init_waitqueue_head(&data[thr].go);
1225 init_waitqueue_head(&data[thr].done);
1227 data[thr].thr = kthread_run(lzo_decompress_threadfn,
1229 "image_decompress/%u", thr);
1230 if (IS_ERR(data[thr].thr)) {
1231 data[thr].thr = NULL;
1232 pr_err("Cannot start decompression threads\n");
1239 * Start the CRC32 thread.
1241 init_waitqueue_head(&crc->go);
1242 init_waitqueue_head(&crc->done);
1245 crc->crc32 = &handle->crc32;
1246 for (thr = 0; thr < nr_threads; thr++) {
1247 crc->unc[thr] = data[thr].unc;
1248 crc->unc_len[thr] = &data[thr].unc_len;
1251 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1252 if (IS_ERR(crc->thr)) {
1254 pr_err("Cannot start CRC32 thread\n");
1260 * Set the number of pages for read buffering.
1261 * This is complete guesswork, because we'll only know the real
1262 * picture once prepare_image() is called, which is much later on
1263 * during the image load phase. We'll assume the worst case and
1264 * say that none of the image pages are from high memory.
1266 if (low_free_pages() > snapshot_get_image_size())
1267 read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1268 read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES);
1270 for (i = 0; i < read_pages; i++) {
1271 page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
1272 GFP_NOIO | __GFP_HIGH :
1273 GFP_NOIO | __GFP_NOWARN |
1277 if (i < LZO_CMP_PAGES) {
1279 pr_err("Failed to allocate LZO pages\n");
1287 want = ring_size = i;
1289 pr_info("Using %u thread(s) for decompression\n", nr_threads);
1290 pr_info("Loading and decompressing image data (%u pages)...\n",
1292 m = nr_to_read / 10;
1296 start = ktime_get();
1298 ret = snapshot_write_next(snapshot);
1303 for (i = 0; !eof && i < want; i++) {
1304 ret = swap_read_page(handle, page[ring], &hb);
1307 * On real read error, finish. On end of data,
1308 * set EOF flag and just exit the read loop.
1311 handle->cur->entries[handle->k]) {
1318 if (++ring >= ring_size)
1325 * We are out of data, wait for some more.
1331 ret = hib_wait_io(&hb);
1340 if (crc->run_threads) {
1341 wait_event(crc->done, atomic_read(&crc->stop));
1342 atomic_set(&crc->stop, 0);
1343 crc->run_threads = 0;
1346 for (thr = 0; have && thr < nr_threads; thr++) {
1347 data[thr].cmp_len = *(size_t *)page[pg];
1348 if (unlikely(!data[thr].cmp_len ||
1350 lzo1x_worst_compress(LZO_UNC_SIZE))) {
1351 pr_err("Invalid LZO compressed length\n");
1356 need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
1367 off < LZO_HEADER + data[thr].cmp_len;
1369 memcpy(data[thr].cmp + off,
1370 page[pg], PAGE_SIZE);
1373 if (++pg >= ring_size)
1377 atomic_set(&data[thr].ready, 1);
1378 wake_up(&data[thr].go);
1382 * Wait for more data while we are decompressing.
1384 if (have < LZO_CMP_PAGES && asked) {
1385 ret = hib_wait_io(&hb);
1394 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1395 wait_event(data[thr].done,
1396 atomic_read(&data[thr].stop));
1397 atomic_set(&data[thr].stop, 0);
1399 ret = data[thr].ret;
1402 pr_err("LZO decompression failed\n");
1406 if (unlikely(!data[thr].unc_len ||
1407 data[thr].unc_len > LZO_UNC_SIZE ||
1408 data[thr].unc_len & (PAGE_SIZE - 1))) {
1409 pr_err("Invalid LZO uncompressed length\n");
1415 off < data[thr].unc_len; off += PAGE_SIZE) {
1416 memcpy(data_of(*snapshot),
1417 data[thr].unc + off, PAGE_SIZE);
1419 if (!(nr_pages % m))
1420 pr_info("Image loading progress: %3d%%\n",
1424 ret = snapshot_write_next(snapshot);
1426 crc->run_threads = thr + 1;
1427 atomic_set(&crc->ready, 1);
1434 crc->run_threads = thr;
1435 atomic_set(&crc->ready, 1);
1440 if (crc->run_threads) {
1441 wait_event(crc->done, atomic_read(&crc->stop));
1442 atomic_set(&crc->stop, 0);
1446 pr_info("Image loading done\n");
1447 snapshot_write_finalize(snapshot);
1448 if (!snapshot_image_loaded(snapshot))
1451 if (swsusp_header->flags & SF_CRC32_MODE) {
1452 if(handle->crc32 != swsusp_header->crc32) {
1453 pr_err("Invalid image CRC32!\n");
1459 swsusp_show_speed(start, stop, nr_to_read, "Read");
1461 hib_finish_batch(&hb);
1462 for (i = 0; i < ring_size; i++)
1463 free_page((unsigned long)page[i]);
1466 kthread_stop(crc->thr);
1470 for (thr = 0; thr < nr_threads; thr++)
1472 kthread_stop(data[thr].thr);
1481 * swsusp_read - read the hibernation image.
1482 * @flags_p: flags passed by the "frozen" kernel in the image header should
1483 * be written into this memory location
1486 int swsusp_read(unsigned int *flags_p)
1489 struct swap_map_handle handle;
1490 struct snapshot_handle snapshot;
1491 struct swsusp_info *header;
1493 memset(&snapshot, 0, sizeof(struct snapshot_handle));
1494 error = snapshot_write_next(&snapshot);
1495 if (error < (int)PAGE_SIZE)
1496 return error < 0 ? error : -EFAULT;
1497 header = (struct swsusp_info *)data_of(snapshot);
1498 error = get_swap_reader(&handle, flags_p);
1502 error = swap_read_page(&handle, header, NULL);
1504 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1505 load_image(&handle, &snapshot, header->pages - 1) :
1506 load_image_lzo(&handle, &snapshot, header->pages - 1);
1508 swap_reader_finish(&handle);
1511 pr_debug("Image successfully loaded\n");
1513 pr_debug("Error %d resuming\n", error);
1518 * swsusp_check - Check for swsusp signature in the resume device
1521 int swsusp_check(void)
1526 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
1527 FMODE_READ | FMODE_EXCL, &holder);
1528 if (!IS_ERR(hib_resume_bdev)) {
1529 set_blocksize(hib_resume_bdev, PAGE_SIZE);
1530 clear_page(swsusp_header);
1531 error = hib_submit_io(REQ_OP_READ, 0,
1532 swsusp_resume_block,
1533 swsusp_header, NULL);
1537 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
1538 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
1539 /* Reset swap signature now */
1540 error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
1541 swsusp_resume_block,
1542 swsusp_header, NULL);
1546 if (!error && swsusp_header->flags & SF_HW_SIG &&
1547 swsusp_header->hw_sig != swsusp_hardware_signature) {
1548 pr_info("Suspend image hardware signature mismatch (%08x now %08x); aborting resume.\n",
1549 swsusp_header->hw_sig, swsusp_hardware_signature);
1555 blkdev_put(hib_resume_bdev, FMODE_READ | FMODE_EXCL);
1557 pr_debug("Image signature found, resuming\n");
1559 error = PTR_ERR(hib_resume_bdev);
1563 pr_debug("Image not found (code %d)\n", error);
1569 * swsusp_close - close swap device.
1572 void swsusp_close(fmode_t mode)
1574 if (IS_ERR(hib_resume_bdev)) {
1575 pr_debug("Image device not initialised\n");
1579 blkdev_put(hib_resume_bdev, mode);
1583 * swsusp_unmark - Unmark swsusp signature in the resume device
1586 #ifdef CONFIG_SUSPEND
1587 int swsusp_unmark(void)
1591 hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
1592 swsusp_header, NULL);
1593 if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1594 memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1595 error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
1596 swsusp_resume_block,
1597 swsusp_header, NULL);
1599 pr_err("Cannot find swsusp signature!\n");
1604 * We just returned from suspend, we don't need the image any more.
1606 free_all_swap_pages(root_swap);
1612 static int __init swsusp_header_init(void)
1614 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1616 panic("Could not allocate memory for swsusp_header\n");
1620 core_initcall(swsusp_header_init);