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/vmalloc.h>
27 #include <linux/cpumask.h>
28 #include <linux/atomic.h>
29 #include <linux/kthread.h>
30 #include <linux/crc32.h>
31 #include <linux/ktime.h>
35 #define HIBERNATE_SIG "S1SUSPEND"
37 u32 swsusp_hardware_signature;
40 * When reading an {un,}compressed image, we may restore pages in place,
41 * in which case some architectures need these pages cleaning before they
42 * can be executed. We don't know which pages these may be, so clean the lot.
44 static bool clean_pages_on_read;
45 static bool clean_pages_on_decompress;
48 * The swap map is a data structure used for keeping track of each page
49 * written to a swap partition. It consists of many swap_map_page
50 * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
51 * These structures are stored on the swap and linked together with the
52 * help of the .next_swap member.
54 * The swap map is created during suspend. The swap map pages are
55 * allocated and populated one at a time, so we only need one memory
56 * page to set up the entire structure.
58 * During resume we pick up all swap_map_page structures into a list.
61 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
64 * Number of free pages that are not high.
66 static inline unsigned long low_free_pages(void)
68 return nr_free_pages() - nr_free_highpages();
72 * Number of pages required to be kept free while writing the image. Always
73 * half of all available low pages before the writing starts.
75 static inline unsigned long reqd_free_pages(void)
77 return low_free_pages() / 2;
80 struct swap_map_page {
81 sector_t entries[MAP_PAGE_ENTRIES];
85 struct swap_map_page_list {
86 struct swap_map_page *map;
87 struct swap_map_page_list *next;
91 * The swap_map_handle structure is used for handling swap in
95 struct swap_map_handle {
96 struct swap_map_page *cur;
97 struct swap_map_page_list *maps;
99 sector_t first_sector;
101 unsigned long reqd_free_pages;
105 struct swsusp_header {
106 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
107 sizeof(u32) - sizeof(u32)];
111 unsigned int flags; /* Flags to pass to the "boot" kernel */
116 static struct swsusp_header *swsusp_header;
119 * The following functions are used for tracing the allocated
120 * swap pages, so that they can be freed in case of an error.
123 struct swsusp_extent {
129 static struct rb_root swsusp_extents = RB_ROOT;
131 static int swsusp_extents_insert(unsigned long swap_offset)
133 struct rb_node **new = &(swsusp_extents.rb_node);
134 struct rb_node *parent = NULL;
135 struct swsusp_extent *ext;
137 /* Figure out where to put the new node */
139 ext = rb_entry(*new, struct swsusp_extent, node);
141 if (swap_offset < ext->start) {
143 if (swap_offset == ext->start - 1) {
147 new = &((*new)->rb_left);
148 } else if (swap_offset > ext->end) {
150 if (swap_offset == ext->end + 1) {
154 new = &((*new)->rb_right);
156 /* It already is in the tree */
160 /* Add the new node and rebalance the tree. */
161 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
165 ext->start = swap_offset;
166 ext->end = swap_offset;
167 rb_link_node(&ext->node, parent, new);
168 rb_insert_color(&ext->node, &swsusp_extents);
173 * alloc_swapdev_block - allocate a swap page and register that it has
174 * been allocated, so that it can be freed in case of an error.
177 sector_t alloc_swapdev_block(int swap)
179 unsigned long offset;
181 offset = swp_offset(get_swap_page_of_type(swap));
183 if (swsusp_extents_insert(offset))
184 swap_free(swp_entry(swap, offset));
186 return swapdev_block(swap, offset);
192 * free_all_swap_pages - free swap pages allocated for saving image data.
193 * It also frees the extents used to register which swap entries had been
197 void free_all_swap_pages(int swap)
199 struct rb_node *node;
201 while ((node = swsusp_extents.rb_node)) {
202 struct swsusp_extent *ext;
203 unsigned long offset;
205 ext = rb_entry(node, struct swsusp_extent, node);
206 rb_erase(node, &swsusp_extents);
207 for (offset = ext->start; offset <= ext->end; offset++)
208 swap_free(swp_entry(swap, offset));
214 int swsusp_swap_in_use(void)
216 return (swsusp_extents.rb_node != NULL);
223 static unsigned short root_swap = 0xffff;
224 static struct file *hib_resume_bdev_file;
226 struct hib_bio_batch {
228 wait_queue_head_t wait;
230 struct blk_plug plug;
233 static void hib_init_batch(struct hib_bio_batch *hb)
235 atomic_set(&hb->count, 0);
236 init_waitqueue_head(&hb->wait);
237 hb->error = BLK_STS_OK;
238 blk_start_plug(&hb->plug);
241 static void hib_finish_batch(struct hib_bio_batch *hb)
243 blk_finish_plug(&hb->plug);
246 static void hib_end_io(struct bio *bio)
248 struct hib_bio_batch *hb = bio->bi_private;
249 struct page *page = bio_first_page_all(bio);
251 if (bio->bi_status) {
252 pr_alert("Read-error on swap-device (%u:%u:%Lu)\n",
253 MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
254 (unsigned long long)bio->bi_iter.bi_sector);
257 if (bio_data_dir(bio) == WRITE)
259 else if (clean_pages_on_read)
260 flush_icache_range((unsigned long)page_address(page),
261 (unsigned long)page_address(page) + PAGE_SIZE);
263 if (bio->bi_status && !hb->error)
264 hb->error = bio->bi_status;
265 if (atomic_dec_and_test(&hb->count))
271 static int hib_submit_io(blk_opf_t opf, pgoff_t page_off, void *addr,
272 struct hib_bio_batch *hb)
274 struct page *page = virt_to_page(addr);
278 bio = bio_alloc(file_bdev(hib_resume_bdev_file), 1, opf,
279 GFP_NOIO | __GFP_HIGH);
280 bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
282 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
283 pr_err("Adding page to bio failed at %llu\n",
284 (unsigned long long)bio->bi_iter.bi_sector);
290 bio->bi_end_io = hib_end_io;
291 bio->bi_private = hb;
292 atomic_inc(&hb->count);
295 error = submit_bio_wait(bio);
302 static int hib_wait_io(struct hib_bio_batch *hb)
305 * We are relying on the behavior of blk_plug that a thread with
306 * a plug will flush the plug list before sleeping.
308 wait_event(hb->wait, atomic_read(&hb->count) == 0);
309 return blk_status_to_errno(hb->error);
315 static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
319 hib_submit_io(REQ_OP_READ, swsusp_resume_block, swsusp_header, NULL);
320 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
321 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
322 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
323 memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
324 swsusp_header->image = handle->first_sector;
325 if (swsusp_hardware_signature) {
326 swsusp_header->hw_sig = swsusp_hardware_signature;
329 swsusp_header->flags = flags;
330 if (flags & SF_CRC32_MODE)
331 swsusp_header->crc32 = handle->crc32;
332 error = hib_submit_io(REQ_OP_WRITE | REQ_SYNC,
333 swsusp_resume_block, swsusp_header, NULL);
335 pr_err("Swap header not found!\n");
342 * Hold the swsusp_header flag. This is used in software_resume() in
343 * 'kernel/power/hibernate' to check if the image is compressed and query
344 * for the compression algorithm support(if so).
346 unsigned int swsusp_header_flags;
349 * swsusp_swap_check - check if the resume device is a swap device
350 * and get its index (if so)
352 * This is called before saving image
354 static int swsusp_swap_check(void)
358 if (swsusp_resume_device)
359 res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
361 res = find_first_swap(&swsusp_resume_device);
366 hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
367 BLK_OPEN_WRITE, NULL, NULL);
368 if (IS_ERR(hib_resume_bdev_file))
369 return PTR_ERR(hib_resume_bdev_file);
371 res = set_blocksize(file_bdev(hib_resume_bdev_file), PAGE_SIZE);
373 fput(hib_resume_bdev_file);
379 * write_page - Write one page to given swap location.
380 * @buf: Address we're writing.
381 * @offset: Offset of the swap page we're writing to.
382 * @hb: bio completion batch
385 static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
394 src = (void *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
399 ret = hib_wait_io(hb); /* Free pages */
402 src = (void *)__get_free_page(GFP_NOIO |
409 hb = NULL; /* Go synchronous */
416 return hib_submit_io(REQ_OP_WRITE | REQ_SYNC, offset, src, hb);
419 static void release_swap_writer(struct swap_map_handle *handle)
422 free_page((unsigned long)handle->cur);
426 static int get_swap_writer(struct swap_map_handle *handle)
430 ret = swsusp_swap_check();
433 pr_err("Cannot find swap device, try swapon -a\n");
436 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
441 handle->cur_swap = alloc_swapdev_block(root_swap);
442 if (!handle->cur_swap) {
447 handle->reqd_free_pages = reqd_free_pages();
448 handle->first_sector = handle->cur_swap;
451 release_swap_writer(handle);
457 static int swap_write_page(struct swap_map_handle *handle, void *buf,
458 struct hib_bio_batch *hb)
465 offset = alloc_swapdev_block(root_swap);
466 error = write_page(buf, offset, hb);
469 handle->cur->entries[handle->k++] = offset;
470 if (handle->k >= MAP_PAGE_ENTRIES) {
471 offset = alloc_swapdev_block(root_swap);
474 handle->cur->next_swap = offset;
475 error = write_page(handle->cur, handle->cur_swap, hb);
478 clear_page(handle->cur);
479 handle->cur_swap = offset;
482 if (hb && low_free_pages() <= handle->reqd_free_pages) {
483 error = hib_wait_io(hb);
487 * Recalculate the number of required free pages, to
488 * make sure we never take more than half.
490 handle->reqd_free_pages = reqd_free_pages();
497 static int flush_swap_writer(struct swap_map_handle *handle)
499 if (handle->cur && handle->cur_swap)
500 return write_page(handle->cur, handle->cur_swap, NULL);
505 static int swap_writer_finish(struct swap_map_handle *handle,
506 unsigned int flags, int error)
510 error = mark_swapfiles(handle, flags);
512 flush_swap_writer(handle);
516 free_all_swap_pages(root_swap);
517 release_swap_writer(handle);
524 * Bytes we need for compressed data in worst case. We assume(limitation)
525 * this is the worst of all the compression algorithms.
527 #define bytes_worst_compress(x) ((x) + ((x) / 16) + 64 + 3 + 2)
529 /* We need to remember how much compressed data we need to read. */
530 #define CMP_HEADER sizeof(size_t)
532 /* Number of pages/bytes we'll compress at one time. */
534 #define UNC_SIZE (UNC_PAGES * PAGE_SIZE)
536 /* Number of pages we need for compressed data (worst case). */
537 #define CMP_PAGES DIV_ROUND_UP(bytes_worst_compress(UNC_SIZE) + \
538 CMP_HEADER, PAGE_SIZE)
539 #define CMP_SIZE (CMP_PAGES * PAGE_SIZE)
541 /* Maximum number of threads for compression/decompression. */
542 #define CMP_THREADS 3
544 /* Minimum/maximum number of pages for read buffering. */
545 #define CMP_MIN_RD_PAGES 1024
546 #define CMP_MAX_RD_PAGES 8192
549 * save_image - save the suspend image data
552 static int save_image(struct swap_map_handle *handle,
553 struct snapshot_handle *snapshot,
554 unsigned int nr_to_write)
560 struct hib_bio_batch hb;
566 pr_info("Saving image data pages (%u pages)...\n",
568 m = nr_to_write / 10;
574 ret = snapshot_read_next(snapshot);
577 ret = swap_write_page(handle, data_of(*snapshot), &hb);
581 pr_info("Image saving progress: %3d%%\n",
585 err2 = hib_wait_io(&hb);
586 hib_finish_batch(&hb);
591 pr_info("Image saving done\n");
592 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
597 * Structure used for CRC32.
600 struct task_struct *thr; /* thread */
601 atomic_t ready; /* ready to start flag */
602 atomic_t stop; /* ready to stop flag */
603 unsigned run_threads; /* nr current threads */
604 wait_queue_head_t go; /* start crc update */
605 wait_queue_head_t done; /* crc update done */
606 u32 *crc32; /* points to handle's crc32 */
607 size_t *unc_len[CMP_THREADS]; /* uncompressed lengths */
608 unsigned char *unc[CMP_THREADS]; /* uncompressed data */
612 * CRC32 update function that runs in its own thread.
614 static int crc32_threadfn(void *data)
616 struct crc_data *d = data;
620 wait_event(d->go, atomic_read_acquire(&d->ready) ||
621 kthread_should_stop());
622 if (kthread_should_stop()) {
624 atomic_set_release(&d->stop, 1);
628 atomic_set(&d->ready, 0);
630 for (i = 0; i < d->run_threads; i++)
631 *d->crc32 = crc32_le(*d->crc32,
632 d->unc[i], *d->unc_len[i]);
633 atomic_set_release(&d->stop, 1);
639 * Structure used for data compression.
642 struct task_struct *thr; /* thread */
643 struct crypto_comp *cc; /* crypto compressor stream */
644 atomic_t ready; /* ready to start flag */
645 atomic_t stop; /* ready to stop flag */
646 int ret; /* return code */
647 wait_queue_head_t go; /* start compression */
648 wait_queue_head_t done; /* compression done */
649 size_t unc_len; /* uncompressed length */
650 size_t cmp_len; /* compressed length */
651 unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
652 unsigned char cmp[CMP_SIZE]; /* compressed buffer */
655 /* Indicates the image size after compression */
656 static atomic_t compressed_size = ATOMIC_INIT(0);
659 * Compression function that runs in its own thread.
661 static int compress_threadfn(void *data)
663 struct cmp_data *d = data;
664 unsigned int cmp_len = 0;
667 wait_event(d->go, atomic_read_acquire(&d->ready) ||
668 kthread_should_stop());
669 if (kthread_should_stop()) {
672 atomic_set_release(&d->stop, 1);
676 atomic_set(&d->ready, 0);
678 cmp_len = CMP_SIZE - CMP_HEADER;
679 d->ret = crypto_comp_compress(d->cc, d->unc, d->unc_len,
682 d->cmp_len = cmp_len;
684 atomic_set(&compressed_size, atomic_read(&compressed_size) + d->cmp_len);
685 atomic_set_release(&d->stop, 1);
692 * save_compressed_image - Save the suspend image data after compression.
693 * @handle: Swap map handle to use for saving the image.
694 * @snapshot: Image to read data from.
695 * @nr_to_write: Number of pages to save.
697 static int save_compressed_image(struct swap_map_handle *handle,
698 struct snapshot_handle *snapshot,
699 unsigned int nr_to_write)
705 struct hib_bio_batch hb;
709 unsigned thr, run_threads, nr_threads;
710 unsigned char *page = NULL;
711 struct cmp_data *data = NULL;
712 struct crc_data *crc = NULL;
716 atomic_set(&compressed_size, 0);
719 * We'll limit the number of threads for compression to limit memory
722 nr_threads = num_online_cpus() - 1;
723 nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
725 page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
727 pr_err("Failed to allocate %s page\n", hib_comp_algo);
732 data = vzalloc(array_size(nr_threads, sizeof(*data)));
734 pr_err("Failed to allocate %s data\n", hib_comp_algo);
739 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
741 pr_err("Failed to allocate crc\n");
747 * Start the compression threads.
749 for (thr = 0; thr < nr_threads; thr++) {
750 init_waitqueue_head(&data[thr].go);
751 init_waitqueue_head(&data[thr].done);
753 data[thr].cc = crypto_alloc_comp(hib_comp_algo, 0, 0);
754 if (IS_ERR_OR_NULL(data[thr].cc)) {
755 pr_err("Could not allocate comp stream %ld\n", PTR_ERR(data[thr].cc));
760 data[thr].thr = kthread_run(compress_threadfn,
762 "image_compress/%u", thr);
763 if (IS_ERR(data[thr].thr)) {
764 data[thr].thr = NULL;
765 pr_err("Cannot start compression threads\n");
772 * Start the CRC32 thread.
774 init_waitqueue_head(&crc->go);
775 init_waitqueue_head(&crc->done);
778 crc->crc32 = &handle->crc32;
779 for (thr = 0; thr < nr_threads; thr++) {
780 crc->unc[thr] = data[thr].unc;
781 crc->unc_len[thr] = &data[thr].unc_len;
784 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
785 if (IS_ERR(crc->thr)) {
787 pr_err("Cannot start CRC32 thread\n");
793 * Adjust the number of required free pages after all allocations have
794 * been done. We don't want to run out of pages when writing.
796 handle->reqd_free_pages = reqd_free_pages();
798 pr_info("Using %u thread(s) for %s compression\n", nr_threads, hib_comp_algo);
799 pr_info("Compressing and saving image data (%u pages)...\n",
801 m = nr_to_write / 10;
807 for (thr = 0; thr < nr_threads; thr++) {
808 for (off = 0; off < UNC_SIZE; off += PAGE_SIZE) {
809 ret = snapshot_read_next(snapshot);
816 memcpy(data[thr].unc + off,
817 data_of(*snapshot), PAGE_SIZE);
820 pr_info("Image saving progress: %3d%%\n",
827 data[thr].unc_len = off;
829 atomic_set_release(&data[thr].ready, 1);
830 wake_up(&data[thr].go);
836 crc->run_threads = thr;
837 atomic_set_release(&crc->ready, 1);
840 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
841 wait_event(data[thr].done,
842 atomic_read_acquire(&data[thr].stop));
843 atomic_set(&data[thr].stop, 0);
848 pr_err("%s compression failed\n", hib_comp_algo);
852 if (unlikely(!data[thr].cmp_len ||
854 bytes_worst_compress(data[thr].unc_len))) {
855 pr_err("Invalid %s compressed length\n", hib_comp_algo);
860 *(size_t *)data[thr].cmp = data[thr].cmp_len;
863 * Given we are writing one page at a time to disk, we
864 * copy that much from the buffer, although the last
865 * bit will likely be smaller than full page. This is
866 * OK - we saved the length of the compressed data, so
867 * any garbage at the end will be discarded when we
871 off < CMP_HEADER + data[thr].cmp_len;
873 memcpy(page, data[thr].cmp + off, PAGE_SIZE);
875 ret = swap_write_page(handle, page, &hb);
881 wait_event(crc->done, atomic_read_acquire(&crc->stop));
882 atomic_set(&crc->stop, 0);
886 err2 = hib_wait_io(&hb);
891 pr_info("Image saving done\n");
892 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
893 pr_info("Image size after compression: %d kbytes\n",
894 (atomic_read(&compressed_size) / 1024));
897 hib_finish_batch(&hb);
900 kthread_stop(crc->thr);
904 for (thr = 0; thr < nr_threads; thr++) {
906 kthread_stop(data[thr].thr);
908 crypto_free_comp(data[thr].cc);
912 if (page) free_page((unsigned long)page);
918 * enough_swap - Make sure we have enough swap to save the image.
920 * Returns TRUE or FALSE after checking the total amount of swap
921 * space available from the resume partition.
924 static int enough_swap(unsigned int nr_pages)
926 unsigned int free_swap = count_swap_pages(root_swap, 1);
927 unsigned int required;
929 pr_debug("Free swap pages: %u\n", free_swap);
931 required = PAGES_FOR_IO + nr_pages;
932 return free_swap > required;
936 * swsusp_write - Write entire image and metadata.
937 * @flags: flags to pass to the "boot" kernel in the image header
939 * It is important _NOT_ to umount filesystems at this point. We want
940 * them synced (in case something goes wrong) but we DO not want to mark
941 * filesystem clean: it is not. (And it does not matter, if we resume
942 * correctly, we'll mark system clean, anyway.)
945 int swsusp_write(unsigned int flags)
947 struct swap_map_handle handle;
948 struct snapshot_handle snapshot;
949 struct swsusp_info *header;
953 pages = snapshot_get_image_size();
954 error = get_swap_writer(&handle);
956 pr_err("Cannot get swap writer\n");
959 if (flags & SF_NOCOMPRESS_MODE) {
960 if (!enough_swap(pages)) {
961 pr_err("Not enough free swap\n");
966 memset(&snapshot, 0, sizeof(struct snapshot_handle));
967 error = snapshot_read_next(&snapshot);
968 if (error < (int)PAGE_SIZE) {
974 header = (struct swsusp_info *)data_of(snapshot);
975 error = swap_write_page(&handle, header, NULL);
977 error = (flags & SF_NOCOMPRESS_MODE) ?
978 save_image(&handle, &snapshot, pages - 1) :
979 save_compressed_image(&handle, &snapshot, pages - 1);
982 error = swap_writer_finish(&handle, flags, error);
987 * The following functions allow us to read data using a swap map
988 * in a file-like way.
991 static void release_swap_reader(struct swap_map_handle *handle)
993 struct swap_map_page_list *tmp;
995 while (handle->maps) {
996 if (handle->maps->map)
997 free_page((unsigned long)handle->maps->map);
999 handle->maps = handle->maps->next;
1005 static int get_swap_reader(struct swap_map_handle *handle,
1006 unsigned int *flags_p)
1009 struct swap_map_page_list *tmp, *last;
1012 *flags_p = swsusp_header->flags;
1014 if (!swsusp_header->image) /* how can this happen? */
1018 last = handle->maps = NULL;
1019 offset = swsusp_header->image;
1021 tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
1023 release_swap_reader(handle);
1032 tmp->map = (struct swap_map_page *)
1033 __get_free_page(GFP_NOIO | __GFP_HIGH);
1035 release_swap_reader(handle);
1039 error = hib_submit_io(REQ_OP_READ, offset, tmp->map, NULL);
1041 release_swap_reader(handle);
1044 offset = tmp->map->next_swap;
1047 handle->cur = handle->maps->map;
1051 static int swap_read_page(struct swap_map_handle *handle, void *buf,
1052 struct hib_bio_batch *hb)
1056 struct swap_map_page_list *tmp;
1060 offset = handle->cur->entries[handle->k];
1063 error = hib_submit_io(REQ_OP_READ, offset, buf, hb);
1066 if (++handle->k >= MAP_PAGE_ENTRIES) {
1068 free_page((unsigned long)handle->maps->map);
1070 handle->maps = handle->maps->next;
1073 release_swap_reader(handle);
1075 handle->cur = handle->maps->map;
1080 static int swap_reader_finish(struct swap_map_handle *handle)
1082 release_swap_reader(handle);
1088 * load_image - load the image using the swap map handle
1089 * @handle and the snapshot handle @snapshot
1090 * (assume there are @nr_pages pages to load)
1093 static int load_image(struct swap_map_handle *handle,
1094 struct snapshot_handle *snapshot,
1095 unsigned int nr_to_read)
1101 struct hib_bio_batch hb;
1105 hib_init_batch(&hb);
1107 clean_pages_on_read = true;
1108 pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
1109 m = nr_to_read / 10;
1113 start = ktime_get();
1115 ret = snapshot_write_next(snapshot);
1118 ret = swap_read_page(handle, data_of(*snapshot), &hb);
1121 if (snapshot->sync_read)
1122 ret = hib_wait_io(&hb);
1125 if (!(nr_pages % m))
1126 pr_info("Image loading progress: %3d%%\n",
1130 err2 = hib_wait_io(&hb);
1131 hib_finish_batch(&hb);
1136 pr_info("Image loading done\n");
1137 ret = snapshot_write_finalize(snapshot);
1138 if (!ret && !snapshot_image_loaded(snapshot))
1141 swsusp_show_speed(start, stop, nr_to_read, "Read");
1146 * Structure used for data decompression.
1149 struct task_struct *thr; /* thread */
1150 struct crypto_comp *cc; /* crypto compressor stream */
1151 atomic_t ready; /* ready to start flag */
1152 atomic_t stop; /* ready to stop flag */
1153 int ret; /* return code */
1154 wait_queue_head_t go; /* start decompression */
1155 wait_queue_head_t done; /* decompression done */
1156 size_t unc_len; /* uncompressed length */
1157 size_t cmp_len; /* compressed length */
1158 unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
1159 unsigned char cmp[CMP_SIZE]; /* compressed buffer */
1163 * Decompression function that runs in its own thread.
1165 static int decompress_threadfn(void *data)
1167 struct dec_data *d = data;
1168 unsigned int unc_len = 0;
1171 wait_event(d->go, atomic_read_acquire(&d->ready) ||
1172 kthread_should_stop());
1173 if (kthread_should_stop()) {
1176 atomic_set_release(&d->stop, 1);
1180 atomic_set(&d->ready, 0);
1183 d->ret = crypto_comp_decompress(d->cc, d->cmp + CMP_HEADER, d->cmp_len,
1185 d->unc_len = unc_len;
1187 if (clean_pages_on_decompress)
1188 flush_icache_range((unsigned long)d->unc,
1189 (unsigned long)d->unc + d->unc_len);
1191 atomic_set_release(&d->stop, 1);
1198 * load_compressed_image - Load compressed image data and decompress it.
1199 * @handle: Swap map handle to use for loading data.
1200 * @snapshot: Image to copy uncompressed data into.
1201 * @nr_to_read: Number of pages to load.
1203 static int load_compressed_image(struct swap_map_handle *handle,
1204 struct snapshot_handle *snapshot,
1205 unsigned int nr_to_read)
1210 struct hib_bio_batch hb;
1215 unsigned i, thr, run_threads, nr_threads;
1216 unsigned ring = 0, pg = 0, ring_size = 0,
1217 have = 0, want, need, asked = 0;
1218 unsigned long read_pages = 0;
1219 unsigned char **page = NULL;
1220 struct dec_data *data = NULL;
1221 struct crc_data *crc = NULL;
1223 hib_init_batch(&hb);
1226 * We'll limit the number of threads for decompression to limit memory
1229 nr_threads = num_online_cpus() - 1;
1230 nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
1232 page = vmalloc(array_size(CMP_MAX_RD_PAGES, sizeof(*page)));
1234 pr_err("Failed to allocate %s page\n", hib_comp_algo);
1239 data = vzalloc(array_size(nr_threads, sizeof(*data)));
1241 pr_err("Failed to allocate %s data\n", hib_comp_algo);
1246 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
1248 pr_err("Failed to allocate crc\n");
1253 clean_pages_on_decompress = true;
1256 * Start the decompression threads.
1258 for (thr = 0; thr < nr_threads; thr++) {
1259 init_waitqueue_head(&data[thr].go);
1260 init_waitqueue_head(&data[thr].done);
1262 data[thr].cc = crypto_alloc_comp(hib_comp_algo, 0, 0);
1263 if (IS_ERR_OR_NULL(data[thr].cc)) {
1264 pr_err("Could not allocate comp stream %ld\n", PTR_ERR(data[thr].cc));
1269 data[thr].thr = kthread_run(decompress_threadfn,
1271 "image_decompress/%u", thr);
1272 if (IS_ERR(data[thr].thr)) {
1273 data[thr].thr = NULL;
1274 pr_err("Cannot start decompression threads\n");
1281 * Start the CRC32 thread.
1283 init_waitqueue_head(&crc->go);
1284 init_waitqueue_head(&crc->done);
1287 crc->crc32 = &handle->crc32;
1288 for (thr = 0; thr < nr_threads; thr++) {
1289 crc->unc[thr] = data[thr].unc;
1290 crc->unc_len[thr] = &data[thr].unc_len;
1293 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1294 if (IS_ERR(crc->thr)) {
1296 pr_err("Cannot start CRC32 thread\n");
1302 * Set the number of pages for read buffering.
1303 * This is complete guesswork, because we'll only know the real
1304 * picture once prepare_image() is called, which is much later on
1305 * during the image load phase. We'll assume the worst case and
1306 * say that none of the image pages are from high memory.
1308 if (low_free_pages() > snapshot_get_image_size())
1309 read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1310 read_pages = clamp_val(read_pages, CMP_MIN_RD_PAGES, CMP_MAX_RD_PAGES);
1312 for (i = 0; i < read_pages; i++) {
1313 page[i] = (void *)__get_free_page(i < CMP_PAGES ?
1314 GFP_NOIO | __GFP_HIGH :
1315 GFP_NOIO | __GFP_NOWARN |
1319 if (i < CMP_PAGES) {
1321 pr_err("Failed to allocate %s pages\n", hib_comp_algo);
1329 want = ring_size = i;
1331 pr_info("Using %u thread(s) for %s decompression\n", nr_threads, hib_comp_algo);
1332 pr_info("Loading and decompressing image data (%u pages)...\n",
1334 m = nr_to_read / 10;
1338 start = ktime_get();
1340 ret = snapshot_write_next(snapshot);
1345 for (i = 0; !eof && i < want; i++) {
1346 ret = swap_read_page(handle, page[ring], &hb);
1349 * On real read error, finish. On end of data,
1350 * set EOF flag and just exit the read loop.
1353 handle->cur->entries[handle->k]) {
1360 if (++ring >= ring_size)
1367 * We are out of data, wait for some more.
1373 ret = hib_wait_io(&hb);
1382 if (crc->run_threads) {
1383 wait_event(crc->done, atomic_read_acquire(&crc->stop));
1384 atomic_set(&crc->stop, 0);
1385 crc->run_threads = 0;
1388 for (thr = 0; have && thr < nr_threads; thr++) {
1389 data[thr].cmp_len = *(size_t *)page[pg];
1390 if (unlikely(!data[thr].cmp_len ||
1392 bytes_worst_compress(UNC_SIZE))) {
1393 pr_err("Invalid %s compressed length\n", hib_comp_algo);
1398 need = DIV_ROUND_UP(data[thr].cmp_len + CMP_HEADER,
1409 off < CMP_HEADER + data[thr].cmp_len;
1411 memcpy(data[thr].cmp + off,
1412 page[pg], PAGE_SIZE);
1415 if (++pg >= ring_size)
1419 atomic_set_release(&data[thr].ready, 1);
1420 wake_up(&data[thr].go);
1424 * Wait for more data while we are decompressing.
1426 if (have < CMP_PAGES && asked) {
1427 ret = hib_wait_io(&hb);
1436 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1437 wait_event(data[thr].done,
1438 atomic_read_acquire(&data[thr].stop));
1439 atomic_set(&data[thr].stop, 0);
1441 ret = data[thr].ret;
1444 pr_err("%s decompression failed\n", hib_comp_algo);
1448 if (unlikely(!data[thr].unc_len ||
1449 data[thr].unc_len > UNC_SIZE ||
1450 data[thr].unc_len & (PAGE_SIZE - 1))) {
1451 pr_err("Invalid %s uncompressed length\n", hib_comp_algo);
1457 off < data[thr].unc_len; off += PAGE_SIZE) {
1458 memcpy(data_of(*snapshot),
1459 data[thr].unc + off, PAGE_SIZE);
1461 if (!(nr_pages % m))
1462 pr_info("Image loading progress: %3d%%\n",
1466 ret = snapshot_write_next(snapshot);
1468 crc->run_threads = thr + 1;
1469 atomic_set_release(&crc->ready, 1);
1476 crc->run_threads = thr;
1477 atomic_set_release(&crc->ready, 1);
1482 if (crc->run_threads) {
1483 wait_event(crc->done, atomic_read_acquire(&crc->stop));
1484 atomic_set(&crc->stop, 0);
1488 pr_info("Image loading done\n");
1489 ret = snapshot_write_finalize(snapshot);
1490 if (!ret && !snapshot_image_loaded(snapshot))
1493 if (swsusp_header->flags & SF_CRC32_MODE) {
1494 if(handle->crc32 != swsusp_header->crc32) {
1495 pr_err("Invalid image CRC32!\n");
1501 swsusp_show_speed(start, stop, nr_to_read, "Read");
1503 hib_finish_batch(&hb);
1504 for (i = 0; i < ring_size; i++)
1505 free_page((unsigned long)page[i]);
1508 kthread_stop(crc->thr);
1512 for (thr = 0; thr < nr_threads; thr++) {
1514 kthread_stop(data[thr].thr);
1516 crypto_free_comp(data[thr].cc);
1526 * swsusp_read - read the hibernation image.
1527 * @flags_p: flags passed by the "frozen" kernel in the image header should
1528 * be written into this memory location
1531 int swsusp_read(unsigned int *flags_p)
1534 struct swap_map_handle handle;
1535 struct snapshot_handle snapshot;
1536 struct swsusp_info *header;
1538 memset(&snapshot, 0, sizeof(struct snapshot_handle));
1539 error = snapshot_write_next(&snapshot);
1540 if (error < (int)PAGE_SIZE)
1541 return error < 0 ? error : -EFAULT;
1542 header = (struct swsusp_info *)data_of(snapshot);
1543 error = get_swap_reader(&handle, flags_p);
1547 error = swap_read_page(&handle, header, NULL);
1549 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1550 load_image(&handle, &snapshot, header->pages - 1) :
1551 load_compressed_image(&handle, &snapshot, header->pages - 1);
1553 swap_reader_finish(&handle);
1556 pr_debug("Image successfully loaded\n");
1558 pr_debug("Error %d resuming\n", error);
1562 static void *swsusp_holder;
1565 * swsusp_check - Open the resume device and check for the swsusp signature.
1566 * @exclusive: Open the resume device exclusively.
1569 int swsusp_check(bool exclusive)
1571 void *holder = exclusive ? &swsusp_holder : NULL;
1574 hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
1575 BLK_OPEN_READ, holder, NULL);
1576 if (!IS_ERR(hib_resume_bdev_file)) {
1577 set_blocksize(file_bdev(hib_resume_bdev_file), PAGE_SIZE);
1578 clear_page(swsusp_header);
1579 error = hib_submit_io(REQ_OP_READ, swsusp_resume_block,
1580 swsusp_header, NULL);
1584 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
1585 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
1586 swsusp_header_flags = swsusp_header->flags;
1587 /* Reset swap signature now */
1588 error = hib_submit_io(REQ_OP_WRITE | REQ_SYNC,
1589 swsusp_resume_block,
1590 swsusp_header, NULL);
1594 if (!error && swsusp_header->flags & SF_HW_SIG &&
1595 swsusp_header->hw_sig != swsusp_hardware_signature) {
1596 pr_info("Suspend image hardware signature mismatch (%08x now %08x); aborting resume.\n",
1597 swsusp_header->hw_sig, swsusp_hardware_signature);
1603 fput(hib_resume_bdev_file);
1605 pr_debug("Image signature found, resuming\n");
1607 error = PTR_ERR(hib_resume_bdev_file);
1611 pr_debug("Image not found (code %d)\n", error);
1617 * swsusp_close - close resume device.
1620 void swsusp_close(void)
1622 if (IS_ERR(hib_resume_bdev_file)) {
1623 pr_debug("Image device not initialised\n");
1627 fput(hib_resume_bdev_file);
1631 * swsusp_unmark - Unmark swsusp signature in the resume device
1634 #ifdef CONFIG_SUSPEND
1635 int swsusp_unmark(void)
1639 hib_submit_io(REQ_OP_READ, swsusp_resume_block,
1640 swsusp_header, NULL);
1641 if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1642 memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1643 error = hib_submit_io(REQ_OP_WRITE | REQ_SYNC,
1644 swsusp_resume_block,
1645 swsusp_header, NULL);
1647 pr_err("Cannot find swsusp signature!\n");
1652 * We just returned from suspend, we don't need the image any more.
1654 free_all_swap_pages(root_swap);
1660 static int __init swsusp_header_init(void)
1662 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1664 panic("Could not allocate memory for swsusp_header\n");
1668 core_initcall(swsusp_header_init);