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 <crypto/acompress.h>
16 #include <linux/module.h>
17 #include <linux/file.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/device.h>
21 #include <linux/bio.h>
22 #include <linux/blkdev.h>
23 #include <linux/swap.h>
24 #include <linux/swapops.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/cpumask.h>
29 #include <linux/atomic.h>
30 #include <linux/kthread.h>
31 #include <linux/crc32.h>
32 #include <linux/ktime.h>
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;
205 ext = rb_entry(node, struct swsusp_extent, node);
206 rb_erase(node, &swsusp_extents);
207 swap_free_nr(swp_entry(swap, ext->start),
208 ext->end - ext->start + 1);
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_sync(blk_opf_t opf, pgoff_t page_off, void *addr)
273 return bdev_rw_virt(file_bdev(hib_resume_bdev_file),
274 page_off * (PAGE_SIZE >> 9), addr, PAGE_SIZE, opf);
277 static int hib_submit_io_async(blk_opf_t opf, pgoff_t page_off, void *addr,
278 struct hib_bio_batch *hb)
282 bio = bio_alloc(file_bdev(hib_resume_bdev_file), 1, opf,
283 GFP_NOIO | __GFP_HIGH);
284 bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
285 bio_add_virt_nofail(bio, addr, PAGE_SIZE);
286 bio->bi_end_io = hib_end_io;
287 bio->bi_private = hb;
288 atomic_inc(&hb->count);
293 static int hib_wait_io(struct hib_bio_batch *hb)
296 * We are relying on the behavior of blk_plug that a thread with
297 * a plug will flush the plug list before sleeping.
299 wait_event(hb->wait, atomic_read(&hb->count) == 0);
300 return blk_status_to_errno(hb->error);
306 static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
310 hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header);
311 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
312 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
313 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
314 memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
315 swsusp_header->image = handle->first_sector;
316 if (swsusp_hardware_signature) {
317 swsusp_header->hw_sig = swsusp_hardware_signature;
320 swsusp_header->flags = flags;
321 if (flags & SF_CRC32_MODE)
322 swsusp_header->crc32 = handle->crc32;
323 error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
324 swsusp_resume_block, swsusp_header);
326 pr_err("Swap header not found!\n");
333 * Hold the swsusp_header flag. This is used in software_resume() in
334 * 'kernel/power/hibernate' to check if the image is compressed and query
335 * for the compression algorithm support(if so).
337 unsigned int swsusp_header_flags;
340 * swsusp_swap_check - check if the resume device is a swap device
341 * and get its index (if so)
343 * This is called before saving image
345 static int swsusp_swap_check(void)
349 if (swsusp_resume_device)
350 res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
352 res = find_first_swap(&swsusp_resume_device);
357 hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
358 BLK_OPEN_WRITE, NULL, NULL);
359 if (IS_ERR(hib_resume_bdev_file))
360 return PTR_ERR(hib_resume_bdev_file);
366 * write_page - Write one page to given swap location.
367 * @buf: Address we're writing.
368 * @offset: Offset of the swap page we're writing to.
369 * @hb: bio completion batch
372 static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
374 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
384 src = (void *)__get_free_page(gfp);
386 ret = hib_wait_io(hb); /* Free pages */
389 src = (void *)__get_free_page(gfp);
390 if (WARN_ON_ONCE(!src))
395 return hib_submit_io_async(REQ_OP_WRITE | REQ_SYNC, offset, src, hb);
397 return hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC, offset, buf);
400 static void release_swap_writer(struct swap_map_handle *handle)
403 free_page((unsigned long)handle->cur);
407 static int get_swap_writer(struct swap_map_handle *handle)
411 ret = swsusp_swap_check();
414 pr_err("Cannot find swap device, try swapon -a\n");
417 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
422 handle->cur_swap = alloc_swapdev_block(root_swap);
423 if (!handle->cur_swap) {
428 handle->reqd_free_pages = reqd_free_pages();
429 handle->first_sector = handle->cur_swap;
432 release_swap_writer(handle);
438 static int swap_write_page(struct swap_map_handle *handle, void *buf,
439 struct hib_bio_batch *hb)
446 offset = alloc_swapdev_block(root_swap);
447 error = write_page(buf, offset, hb);
450 handle->cur->entries[handle->k++] = offset;
451 if (handle->k >= MAP_PAGE_ENTRIES) {
452 offset = alloc_swapdev_block(root_swap);
455 handle->cur->next_swap = offset;
456 error = write_page(handle->cur, handle->cur_swap, hb);
459 clear_page(handle->cur);
460 handle->cur_swap = offset;
463 if (hb && low_free_pages() <= handle->reqd_free_pages) {
464 error = hib_wait_io(hb);
468 * Recalculate the number of required free pages, to
469 * make sure we never take more than half.
471 handle->reqd_free_pages = reqd_free_pages();
478 static int flush_swap_writer(struct swap_map_handle *handle)
480 if (handle->cur && handle->cur_swap)
481 return write_page(handle->cur, handle->cur_swap, NULL);
486 static int swap_writer_finish(struct swap_map_handle *handle,
487 unsigned int flags, int error)
491 error = mark_swapfiles(handle, flags);
493 flush_swap_writer(handle);
497 free_all_swap_pages(root_swap);
498 release_swap_writer(handle);
505 * Bytes we need for compressed data in worst case. We assume(limitation)
506 * this is the worst of all the compression algorithms.
508 #define bytes_worst_compress(x) ((x) + ((x) / 16) + 64 + 3 + 2)
510 /* We need to remember how much compressed data we need to read. */
511 #define CMP_HEADER sizeof(size_t)
513 /* Number of pages/bytes we'll compress at one time. */
515 #define UNC_SIZE (UNC_PAGES * PAGE_SIZE)
517 /* Number of pages we need for compressed data (worst case). */
518 #define CMP_PAGES DIV_ROUND_UP(bytes_worst_compress(UNC_SIZE) + \
519 CMP_HEADER, PAGE_SIZE)
520 #define CMP_SIZE (CMP_PAGES * PAGE_SIZE)
522 /* Maximum number of threads for compression/decompression. */
523 #define CMP_THREADS 3
525 /* Minimum/maximum number of pages for read buffering. */
526 #define CMP_MIN_RD_PAGES 1024
527 #define CMP_MAX_RD_PAGES 8192
530 * save_image - save the suspend image data
533 static int save_image(struct swap_map_handle *handle,
534 struct snapshot_handle *snapshot,
535 unsigned int nr_to_write)
541 struct hib_bio_batch hb;
547 pr_info("Saving image data pages (%u pages)...\n",
549 m = nr_to_write / 10;
555 ret = snapshot_read_next(snapshot);
558 ret = swap_write_page(handle, data_of(*snapshot), &hb);
562 pr_info("Image saving progress: %3d%%\n",
566 err2 = hib_wait_io(&hb);
567 hib_finish_batch(&hb);
572 pr_info("Image saving done\n");
573 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
578 * Structure used for CRC32.
581 struct task_struct *thr; /* thread */
582 atomic_t ready; /* ready to start flag */
583 atomic_t stop; /* ready to stop flag */
584 unsigned run_threads; /* nr current threads */
585 wait_queue_head_t go; /* start crc update */
586 wait_queue_head_t done; /* crc update done */
587 u32 *crc32; /* points to handle's crc32 */
588 size_t *unc_len[CMP_THREADS]; /* uncompressed lengths */
589 unsigned char *unc[CMP_THREADS]; /* uncompressed data */
593 * CRC32 update function that runs in its own thread.
595 static int crc32_threadfn(void *data)
597 struct crc_data *d = data;
601 wait_event(d->go, atomic_read_acquire(&d->ready) ||
602 kthread_should_stop());
603 if (kthread_should_stop()) {
605 atomic_set_release(&d->stop, 1);
609 atomic_set(&d->ready, 0);
611 for (i = 0; i < d->run_threads; i++)
612 *d->crc32 = crc32_le(*d->crc32,
613 d->unc[i], *d->unc_len[i]);
614 atomic_set_release(&d->stop, 1);
620 * Structure used for data compression.
623 struct task_struct *thr; /* thread */
624 struct crypto_acomp *cc; /* crypto compressor */
625 struct acomp_req *cr; /* crypto request */
626 atomic_t ready; /* ready to start flag */
627 atomic_t stop; /* ready to stop flag */
628 int ret; /* return code */
629 wait_queue_head_t go; /* start compression */
630 wait_queue_head_t done; /* compression done */
631 size_t unc_len; /* uncompressed length */
632 size_t cmp_len; /* compressed length */
633 unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
634 unsigned char cmp[CMP_SIZE]; /* compressed buffer */
637 /* Indicates the image size after compression */
638 static atomic_t compressed_size = ATOMIC_INIT(0);
641 * Compression function that runs in its own thread.
643 static int compress_threadfn(void *data)
645 struct cmp_data *d = data;
648 wait_event(d->go, atomic_read_acquire(&d->ready) ||
649 kthread_should_stop());
650 if (kthread_should_stop()) {
653 atomic_set_release(&d->stop, 1);
657 atomic_set(&d->ready, 0);
659 acomp_request_set_callback(d->cr, CRYPTO_TFM_REQ_MAY_SLEEP,
661 acomp_request_set_src_nondma(d->cr, d->unc, d->unc_len);
662 acomp_request_set_dst_nondma(d->cr, d->cmp + CMP_HEADER,
663 CMP_SIZE - CMP_HEADER);
664 d->ret = crypto_acomp_compress(d->cr);
665 d->cmp_len = d->cr->dlen;
667 atomic_set(&compressed_size, atomic_read(&compressed_size) + d->cmp_len);
668 atomic_set_release(&d->stop, 1);
675 * save_compressed_image - Save the suspend image data after compression.
676 * @handle: Swap map handle to use for saving the image.
677 * @snapshot: Image to read data from.
678 * @nr_to_write: Number of pages to save.
680 static int save_compressed_image(struct swap_map_handle *handle,
681 struct snapshot_handle *snapshot,
682 unsigned int nr_to_write)
688 struct hib_bio_batch hb;
692 unsigned thr, run_threads, nr_threads;
693 unsigned char *page = NULL;
694 struct cmp_data *data = NULL;
695 struct crc_data *crc = NULL;
699 atomic_set(&compressed_size, 0);
702 * We'll limit the number of threads for compression to limit memory
705 nr_threads = num_online_cpus() - 1;
706 nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
708 page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
710 pr_err("Failed to allocate %s page\n", hib_comp_algo);
715 data = vzalloc(array_size(nr_threads, sizeof(*data)));
717 pr_err("Failed to allocate %s data\n", hib_comp_algo);
722 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
724 pr_err("Failed to allocate crc\n");
730 * Start the compression threads.
732 for (thr = 0; thr < nr_threads; thr++) {
733 init_waitqueue_head(&data[thr].go);
734 init_waitqueue_head(&data[thr].done);
736 data[thr].cc = crypto_alloc_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC);
737 if (IS_ERR_OR_NULL(data[thr].cc)) {
738 pr_err("Could not allocate comp stream %ld\n", PTR_ERR(data[thr].cc));
743 data[thr].cr = acomp_request_alloc(data[thr].cc);
745 pr_err("Could not allocate comp request\n");
750 data[thr].thr = kthread_run(compress_threadfn,
752 "image_compress/%u", thr);
753 if (IS_ERR(data[thr].thr)) {
754 data[thr].thr = NULL;
755 pr_err("Cannot start compression threads\n");
762 * Start the CRC32 thread.
764 init_waitqueue_head(&crc->go);
765 init_waitqueue_head(&crc->done);
768 crc->crc32 = &handle->crc32;
769 for (thr = 0; thr < nr_threads; thr++) {
770 crc->unc[thr] = data[thr].unc;
771 crc->unc_len[thr] = &data[thr].unc_len;
774 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
775 if (IS_ERR(crc->thr)) {
777 pr_err("Cannot start CRC32 thread\n");
783 * Adjust the number of required free pages after all allocations have
784 * been done. We don't want to run out of pages when writing.
786 handle->reqd_free_pages = reqd_free_pages();
788 pr_info("Using %u thread(s) for %s compression\n", nr_threads, hib_comp_algo);
789 pr_info("Compressing and saving image data (%u pages)...\n",
791 m = nr_to_write / 10;
797 for (thr = 0; thr < nr_threads; thr++) {
798 for (off = 0; off < UNC_SIZE; off += PAGE_SIZE) {
799 ret = snapshot_read_next(snapshot);
806 memcpy(data[thr].unc + off,
807 data_of(*snapshot), PAGE_SIZE);
810 pr_info("Image saving progress: %3d%%\n",
817 data[thr].unc_len = off;
819 atomic_set_release(&data[thr].ready, 1);
820 wake_up(&data[thr].go);
826 crc->run_threads = thr;
827 atomic_set_release(&crc->ready, 1);
830 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
831 wait_event(data[thr].done,
832 atomic_read_acquire(&data[thr].stop));
833 atomic_set(&data[thr].stop, 0);
838 pr_err("%s compression failed\n", hib_comp_algo);
842 if (unlikely(!data[thr].cmp_len ||
844 bytes_worst_compress(data[thr].unc_len))) {
845 pr_err("Invalid %s compressed length\n", hib_comp_algo);
850 *(size_t *)data[thr].cmp = data[thr].cmp_len;
853 * Given we are writing one page at a time to disk, we
854 * copy that much from the buffer, although the last
855 * bit will likely be smaller than full page. This is
856 * OK - we saved the length of the compressed data, so
857 * any garbage at the end will be discarded when we
861 off < CMP_HEADER + data[thr].cmp_len;
863 memcpy(page, data[thr].cmp + off, PAGE_SIZE);
865 ret = swap_write_page(handle, page, &hb);
871 wait_event(crc->done, atomic_read_acquire(&crc->stop));
872 atomic_set(&crc->stop, 0);
876 err2 = hib_wait_io(&hb);
881 pr_info("Image saving done\n");
882 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
883 pr_info("Image size after compression: %d kbytes\n",
884 (atomic_read(&compressed_size) / 1024));
887 hib_finish_batch(&hb);
890 kthread_stop(crc->thr);
894 for (thr = 0; thr < nr_threads; thr++) {
896 kthread_stop(data[thr].thr);
897 acomp_request_free(data[thr].cr);
898 crypto_free_acomp(data[thr].cc);
902 if (page) free_page((unsigned long)page);
908 * enough_swap - Make sure we have enough swap to save the image.
910 * Returns TRUE or FALSE after checking the total amount of swap
911 * space available from the resume partition.
914 static int enough_swap(unsigned int nr_pages)
916 unsigned int free_swap = count_swap_pages(root_swap, 1);
917 unsigned int required;
919 pr_debug("Free swap pages: %u\n", free_swap);
921 required = PAGES_FOR_IO + nr_pages;
922 return free_swap > required;
926 * swsusp_write - Write entire image and metadata.
927 * @flags: flags to pass to the "boot" kernel in the image header
929 * It is important _NOT_ to umount filesystems at this point. We want
930 * them synced (in case something goes wrong) but we DO not want to mark
931 * filesystem clean: it is not. (And it does not matter, if we resume
932 * correctly, we'll mark system clean, anyway.)
935 int swsusp_write(unsigned int flags)
937 struct swap_map_handle handle;
938 struct snapshot_handle snapshot;
939 struct swsusp_info *header;
943 pages = snapshot_get_image_size();
944 error = get_swap_writer(&handle);
946 pr_err("Cannot get swap writer\n");
949 if (flags & SF_NOCOMPRESS_MODE) {
950 if (!enough_swap(pages)) {
951 pr_err("Not enough free swap\n");
956 memset(&snapshot, 0, sizeof(struct snapshot_handle));
957 error = snapshot_read_next(&snapshot);
958 if (error < (int)PAGE_SIZE) {
964 header = (struct swsusp_info *)data_of(snapshot);
965 error = swap_write_page(&handle, header, NULL);
967 error = (flags & SF_NOCOMPRESS_MODE) ?
968 save_image(&handle, &snapshot, pages - 1) :
969 save_compressed_image(&handle, &snapshot, pages - 1);
972 error = swap_writer_finish(&handle, flags, error);
977 * The following functions allow us to read data using a swap map
978 * in a file-like way.
981 static void release_swap_reader(struct swap_map_handle *handle)
983 struct swap_map_page_list *tmp;
985 while (handle->maps) {
986 if (handle->maps->map)
987 free_page((unsigned long)handle->maps->map);
989 handle->maps = handle->maps->next;
995 static int get_swap_reader(struct swap_map_handle *handle,
996 unsigned int *flags_p)
999 struct swap_map_page_list *tmp, *last;
1002 *flags_p = swsusp_header->flags;
1004 if (!swsusp_header->image) /* how can this happen? */
1008 last = handle->maps = NULL;
1009 offset = swsusp_header->image;
1011 tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
1013 release_swap_reader(handle);
1022 tmp->map = (struct swap_map_page *)
1023 __get_free_page(GFP_NOIO | __GFP_HIGH);
1025 release_swap_reader(handle);
1029 error = hib_submit_io_sync(REQ_OP_READ, offset, tmp->map);
1031 release_swap_reader(handle);
1034 offset = tmp->map->next_swap;
1037 handle->cur = handle->maps->map;
1041 static int swap_read_page(struct swap_map_handle *handle, void *buf,
1042 struct hib_bio_batch *hb)
1046 struct swap_map_page_list *tmp;
1050 offset = handle->cur->entries[handle->k];
1054 error = hib_submit_io_async(REQ_OP_READ, offset, buf, hb);
1056 error = hib_submit_io_sync(REQ_OP_READ, offset, buf);
1059 if (++handle->k >= MAP_PAGE_ENTRIES) {
1061 free_page((unsigned long)handle->maps->map);
1063 handle->maps = handle->maps->next;
1066 release_swap_reader(handle);
1068 handle->cur = handle->maps->map;
1073 static int swap_reader_finish(struct swap_map_handle *handle)
1075 release_swap_reader(handle);
1081 * load_image - load the image using the swap map handle
1082 * @handle and the snapshot handle @snapshot
1083 * (assume there are @nr_pages pages to load)
1086 static int load_image(struct swap_map_handle *handle,
1087 struct snapshot_handle *snapshot,
1088 unsigned int nr_to_read)
1094 struct hib_bio_batch hb;
1098 hib_init_batch(&hb);
1100 clean_pages_on_read = true;
1101 pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
1102 m = nr_to_read / 10;
1106 start = ktime_get();
1108 ret = snapshot_write_next(snapshot);
1111 ret = swap_read_page(handle, data_of(*snapshot), &hb);
1114 if (snapshot->sync_read)
1115 ret = hib_wait_io(&hb);
1118 if (!(nr_pages % m))
1119 pr_info("Image loading progress: %3d%%\n",
1123 err2 = hib_wait_io(&hb);
1124 hib_finish_batch(&hb);
1129 pr_info("Image loading done\n");
1130 ret = snapshot_write_finalize(snapshot);
1131 if (!ret && !snapshot_image_loaded(snapshot))
1134 swsusp_show_speed(start, stop, nr_to_read, "Read");
1139 * Structure used for data decompression.
1142 struct task_struct *thr; /* thread */
1143 struct crypto_acomp *cc; /* crypto compressor */
1144 struct acomp_req *cr; /* crypto request */
1145 atomic_t ready; /* ready to start flag */
1146 atomic_t stop; /* ready to stop flag */
1147 int ret; /* return code */
1148 wait_queue_head_t go; /* start decompression */
1149 wait_queue_head_t done; /* decompression done */
1150 size_t unc_len; /* uncompressed length */
1151 size_t cmp_len; /* compressed length */
1152 unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
1153 unsigned char cmp[CMP_SIZE]; /* compressed buffer */
1157 * Decompression function that runs in its own thread.
1159 static int decompress_threadfn(void *data)
1161 struct dec_data *d = data;
1164 wait_event(d->go, atomic_read_acquire(&d->ready) ||
1165 kthread_should_stop());
1166 if (kthread_should_stop()) {
1169 atomic_set_release(&d->stop, 1);
1173 atomic_set(&d->ready, 0);
1175 acomp_request_set_callback(d->cr, CRYPTO_TFM_REQ_MAY_SLEEP,
1177 acomp_request_set_src_nondma(d->cr, d->cmp + CMP_HEADER,
1179 acomp_request_set_dst_nondma(d->cr, d->unc, UNC_SIZE);
1180 d->ret = crypto_acomp_decompress(d->cr);
1181 d->unc_len = d->cr->dlen;
1183 if (clean_pages_on_decompress)
1184 flush_icache_range((unsigned long)d->unc,
1185 (unsigned long)d->unc + d->unc_len);
1187 atomic_set_release(&d->stop, 1);
1194 * load_compressed_image - Load compressed image data and decompress it.
1195 * @handle: Swap map handle to use for loading data.
1196 * @snapshot: Image to copy uncompressed data into.
1197 * @nr_to_read: Number of pages to load.
1199 static int load_compressed_image(struct swap_map_handle *handle,
1200 struct snapshot_handle *snapshot,
1201 unsigned int nr_to_read)
1206 struct hib_bio_batch hb;
1211 unsigned i, thr, run_threads, nr_threads;
1212 unsigned ring = 0, pg = 0, ring_size = 0,
1213 have = 0, want, need, asked = 0;
1214 unsigned long read_pages = 0;
1215 unsigned char **page = NULL;
1216 struct dec_data *data = NULL;
1217 struct crc_data *crc = NULL;
1219 hib_init_batch(&hb);
1222 * We'll limit the number of threads for decompression to limit memory
1225 nr_threads = num_online_cpus() - 1;
1226 nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
1228 page = vmalloc(array_size(CMP_MAX_RD_PAGES, sizeof(*page)));
1230 pr_err("Failed to allocate %s page\n", hib_comp_algo);
1235 data = vzalloc(array_size(nr_threads, sizeof(*data)));
1237 pr_err("Failed to allocate %s data\n", hib_comp_algo);
1242 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
1244 pr_err("Failed to allocate crc\n");
1249 clean_pages_on_decompress = true;
1252 * Start the decompression threads.
1254 for (thr = 0; thr < nr_threads; thr++) {
1255 init_waitqueue_head(&data[thr].go);
1256 init_waitqueue_head(&data[thr].done);
1258 data[thr].cc = crypto_alloc_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC);
1259 if (IS_ERR_OR_NULL(data[thr].cc)) {
1260 pr_err("Could not allocate comp stream %ld\n", PTR_ERR(data[thr].cc));
1265 data[thr].cr = acomp_request_alloc(data[thr].cc);
1266 if (!data[thr].cr) {
1267 pr_err("Could not allocate comp request\n");
1272 data[thr].thr = kthread_run(decompress_threadfn,
1274 "image_decompress/%u", thr);
1275 if (IS_ERR(data[thr].thr)) {
1276 data[thr].thr = NULL;
1277 pr_err("Cannot start decompression threads\n");
1284 * Start the CRC32 thread.
1286 init_waitqueue_head(&crc->go);
1287 init_waitqueue_head(&crc->done);
1290 crc->crc32 = &handle->crc32;
1291 for (thr = 0; thr < nr_threads; thr++) {
1292 crc->unc[thr] = data[thr].unc;
1293 crc->unc_len[thr] = &data[thr].unc_len;
1296 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1297 if (IS_ERR(crc->thr)) {
1299 pr_err("Cannot start CRC32 thread\n");
1305 * Set the number of pages for read buffering.
1306 * This is complete guesswork, because we'll only know the real
1307 * picture once prepare_image() is called, which is much later on
1308 * during the image load phase. We'll assume the worst case and
1309 * say that none of the image pages are from high memory.
1311 if (low_free_pages() > snapshot_get_image_size())
1312 read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1313 read_pages = clamp_val(read_pages, CMP_MIN_RD_PAGES, CMP_MAX_RD_PAGES);
1315 for (i = 0; i < read_pages; i++) {
1316 page[i] = (void *)__get_free_page(i < CMP_PAGES ?
1317 GFP_NOIO | __GFP_HIGH :
1318 GFP_NOIO | __GFP_NOWARN |
1322 if (i < CMP_PAGES) {
1324 pr_err("Failed to allocate %s pages\n", hib_comp_algo);
1332 want = ring_size = i;
1334 pr_info("Using %u thread(s) for %s decompression\n", nr_threads, hib_comp_algo);
1335 pr_info("Loading and decompressing image data (%u pages)...\n",
1337 m = nr_to_read / 10;
1341 start = ktime_get();
1343 ret = snapshot_write_next(snapshot);
1348 for (i = 0; !eof && i < want; i++) {
1349 ret = swap_read_page(handle, page[ring], &hb);
1352 * On real read error, finish. On end of data,
1353 * set EOF flag and just exit the read loop.
1356 handle->cur->entries[handle->k]) {
1363 if (++ring >= ring_size)
1370 * We are out of data, wait for some more.
1376 ret = hib_wait_io(&hb);
1385 if (crc->run_threads) {
1386 wait_event(crc->done, atomic_read_acquire(&crc->stop));
1387 atomic_set(&crc->stop, 0);
1388 crc->run_threads = 0;
1391 for (thr = 0; have && thr < nr_threads; thr++) {
1392 data[thr].cmp_len = *(size_t *)page[pg];
1393 if (unlikely(!data[thr].cmp_len ||
1395 bytes_worst_compress(UNC_SIZE))) {
1396 pr_err("Invalid %s compressed length\n", hib_comp_algo);
1401 need = DIV_ROUND_UP(data[thr].cmp_len + CMP_HEADER,
1412 off < CMP_HEADER + data[thr].cmp_len;
1414 memcpy(data[thr].cmp + off,
1415 page[pg], PAGE_SIZE);
1418 if (++pg >= ring_size)
1422 atomic_set_release(&data[thr].ready, 1);
1423 wake_up(&data[thr].go);
1427 * Wait for more data while we are decompressing.
1429 if (have < CMP_PAGES && asked) {
1430 ret = hib_wait_io(&hb);
1439 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1440 wait_event(data[thr].done,
1441 atomic_read_acquire(&data[thr].stop));
1442 atomic_set(&data[thr].stop, 0);
1444 ret = data[thr].ret;
1447 pr_err("%s decompression failed\n", hib_comp_algo);
1451 if (unlikely(!data[thr].unc_len ||
1452 data[thr].unc_len > UNC_SIZE ||
1453 data[thr].unc_len & (PAGE_SIZE - 1))) {
1454 pr_err("Invalid %s uncompressed length\n", hib_comp_algo);
1460 off < data[thr].unc_len; off += PAGE_SIZE) {
1461 memcpy(data_of(*snapshot),
1462 data[thr].unc + off, PAGE_SIZE);
1464 if (!(nr_pages % m))
1465 pr_info("Image loading progress: %3d%%\n",
1469 ret = snapshot_write_next(snapshot);
1471 crc->run_threads = thr + 1;
1472 atomic_set_release(&crc->ready, 1);
1479 crc->run_threads = thr;
1480 atomic_set_release(&crc->ready, 1);
1485 if (crc->run_threads) {
1486 wait_event(crc->done, atomic_read_acquire(&crc->stop));
1487 atomic_set(&crc->stop, 0);
1491 pr_info("Image loading done\n");
1492 ret = snapshot_write_finalize(snapshot);
1493 if (!ret && !snapshot_image_loaded(snapshot))
1496 if (swsusp_header->flags & SF_CRC32_MODE) {
1497 if(handle->crc32 != swsusp_header->crc32) {
1498 pr_err("Invalid image CRC32!\n");
1504 swsusp_show_speed(start, stop, nr_to_read, "Read");
1506 hib_finish_batch(&hb);
1507 for (i = 0; i < ring_size; i++)
1508 free_page((unsigned long)page[i]);
1511 kthread_stop(crc->thr);
1515 for (thr = 0; thr < nr_threads; thr++) {
1517 kthread_stop(data[thr].thr);
1518 acomp_request_free(data[thr].cr);
1519 crypto_free_acomp(data[thr].cc);
1529 * swsusp_read - read the hibernation image.
1530 * @flags_p: flags passed by the "frozen" kernel in the image header should
1531 * be written into this memory location
1534 int swsusp_read(unsigned int *flags_p)
1537 struct swap_map_handle handle;
1538 struct snapshot_handle snapshot;
1539 struct swsusp_info *header;
1541 memset(&snapshot, 0, sizeof(struct snapshot_handle));
1542 error = snapshot_write_next(&snapshot);
1543 if (error < (int)PAGE_SIZE)
1544 return error < 0 ? error : -EFAULT;
1545 header = (struct swsusp_info *)data_of(snapshot);
1546 error = get_swap_reader(&handle, flags_p);
1550 error = swap_read_page(&handle, header, NULL);
1552 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1553 load_image(&handle, &snapshot, header->pages - 1) :
1554 load_compressed_image(&handle, &snapshot, header->pages - 1);
1556 swap_reader_finish(&handle);
1559 pr_debug("Image successfully loaded\n");
1561 pr_debug("Error %d resuming\n", error);
1565 static void *swsusp_holder;
1568 * swsusp_check - Open the resume device and check for the swsusp signature.
1569 * @exclusive: Open the resume device exclusively.
1572 int swsusp_check(bool exclusive)
1574 void *holder = exclusive ? &swsusp_holder : NULL;
1577 hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
1578 BLK_OPEN_READ, holder, NULL);
1579 if (!IS_ERR(hib_resume_bdev_file)) {
1580 clear_page(swsusp_header);
1581 error = hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block,
1586 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
1587 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
1588 swsusp_header_flags = swsusp_header->flags;
1589 /* Reset swap signature now */
1590 error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
1591 swsusp_resume_block,
1596 if (!error && swsusp_header->flags & SF_HW_SIG &&
1597 swsusp_header->hw_sig != swsusp_hardware_signature) {
1598 pr_info("Suspend image hardware signature mismatch (%08x now %08x); aborting resume.\n",
1599 swsusp_header->hw_sig, swsusp_hardware_signature);
1605 bdev_fput(hib_resume_bdev_file);
1607 pr_debug("Image signature found, resuming\n");
1609 error = PTR_ERR(hib_resume_bdev_file);
1613 pr_debug("Image not found (code %d)\n", error);
1619 * swsusp_close - close resume device.
1622 void swsusp_close(void)
1624 if (IS_ERR(hib_resume_bdev_file)) {
1625 pr_debug("Image device not initialised\n");
1629 fput(hib_resume_bdev_file);
1633 * swsusp_unmark - Unmark swsusp signature in the resume device
1636 #ifdef CONFIG_SUSPEND
1637 int swsusp_unmark(void)
1641 hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header);
1642 if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1643 memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1644 error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
1645 swsusp_resume_block,
1648 pr_err("Cannot find swsusp signature!\n");
1653 * We just returned from suspend, we don't need the image any more.
1655 free_all_swap_pages(root_swap);
1661 static int __init swsusp_header_init(void)
1663 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1665 panic("Could not allocate memory for swsusp_header\n");
1669 core_initcall(swsusp_header_init);