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.
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);
375 * write_page - Write one page to given swap location.
376 * @buf: Address we're writing.
377 * @offset: Offset of the swap page we're writing to.
378 * @hb: bio completion batch
381 static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
390 src = (void *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
395 ret = hib_wait_io(hb); /* Free pages */
398 src = (void *)__get_free_page(GFP_NOIO |
405 hb = NULL; /* Go synchronous */
412 return hib_submit_io(REQ_OP_WRITE | REQ_SYNC, offset, src, hb);
415 static void release_swap_writer(struct swap_map_handle *handle)
418 free_page((unsigned long)handle->cur);
422 static int get_swap_writer(struct swap_map_handle *handle)
426 ret = swsusp_swap_check();
429 pr_err("Cannot find swap device, try swapon -a\n");
432 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
437 handle->cur_swap = alloc_swapdev_block(root_swap);
438 if (!handle->cur_swap) {
443 handle->reqd_free_pages = reqd_free_pages();
444 handle->first_sector = handle->cur_swap;
447 release_swap_writer(handle);
453 static int swap_write_page(struct swap_map_handle *handle, void *buf,
454 struct hib_bio_batch *hb)
461 offset = alloc_swapdev_block(root_swap);
462 error = write_page(buf, offset, hb);
465 handle->cur->entries[handle->k++] = offset;
466 if (handle->k >= MAP_PAGE_ENTRIES) {
467 offset = alloc_swapdev_block(root_swap);
470 handle->cur->next_swap = offset;
471 error = write_page(handle->cur, handle->cur_swap, hb);
474 clear_page(handle->cur);
475 handle->cur_swap = offset;
478 if (hb && low_free_pages() <= handle->reqd_free_pages) {
479 error = hib_wait_io(hb);
483 * Recalculate the number of required free pages, to
484 * make sure we never take more than half.
486 handle->reqd_free_pages = reqd_free_pages();
493 static int flush_swap_writer(struct swap_map_handle *handle)
495 if (handle->cur && handle->cur_swap)
496 return write_page(handle->cur, handle->cur_swap, NULL);
501 static int swap_writer_finish(struct swap_map_handle *handle,
502 unsigned int flags, int error)
506 error = mark_swapfiles(handle, flags);
508 flush_swap_writer(handle);
512 free_all_swap_pages(root_swap);
513 release_swap_writer(handle);
520 * Bytes we need for compressed data in worst case. We assume(limitation)
521 * this is the worst of all the compression algorithms.
523 #define bytes_worst_compress(x) ((x) + ((x) / 16) + 64 + 3 + 2)
525 /* We need to remember how much compressed data we need to read. */
526 #define CMP_HEADER sizeof(size_t)
528 /* Number of pages/bytes we'll compress at one time. */
530 #define UNC_SIZE (UNC_PAGES * PAGE_SIZE)
532 /* Number of pages we need for compressed data (worst case). */
533 #define CMP_PAGES DIV_ROUND_UP(bytes_worst_compress(UNC_SIZE) + \
534 CMP_HEADER, PAGE_SIZE)
535 #define CMP_SIZE (CMP_PAGES * PAGE_SIZE)
537 /* Maximum number of threads for compression/decompression. */
538 #define CMP_THREADS 3
540 /* Minimum/maximum number of pages for read buffering. */
541 #define CMP_MIN_RD_PAGES 1024
542 #define CMP_MAX_RD_PAGES 8192
545 * save_image - save the suspend image data
548 static int save_image(struct swap_map_handle *handle,
549 struct snapshot_handle *snapshot,
550 unsigned int nr_to_write)
556 struct hib_bio_batch hb;
562 pr_info("Saving image data pages (%u pages)...\n",
564 m = nr_to_write / 10;
570 ret = snapshot_read_next(snapshot);
573 ret = swap_write_page(handle, data_of(*snapshot), &hb);
577 pr_info("Image saving progress: %3d%%\n",
581 err2 = hib_wait_io(&hb);
582 hib_finish_batch(&hb);
587 pr_info("Image saving done\n");
588 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
593 * Structure used for CRC32.
596 struct task_struct *thr; /* thread */
597 atomic_t ready; /* ready to start flag */
598 atomic_t stop; /* ready to stop flag */
599 unsigned run_threads; /* nr current threads */
600 wait_queue_head_t go; /* start crc update */
601 wait_queue_head_t done; /* crc update done */
602 u32 *crc32; /* points to handle's crc32 */
603 size_t *unc_len[CMP_THREADS]; /* uncompressed lengths */
604 unsigned char *unc[CMP_THREADS]; /* uncompressed data */
608 * CRC32 update function that runs in its own thread.
610 static int crc32_threadfn(void *data)
612 struct crc_data *d = data;
616 wait_event(d->go, atomic_read_acquire(&d->ready) ||
617 kthread_should_stop());
618 if (kthread_should_stop()) {
620 atomic_set_release(&d->stop, 1);
624 atomic_set(&d->ready, 0);
626 for (i = 0; i < d->run_threads; i++)
627 *d->crc32 = crc32_le(*d->crc32,
628 d->unc[i], *d->unc_len[i]);
629 atomic_set_release(&d->stop, 1);
635 * Structure used for data compression.
638 struct task_struct *thr; /* thread */
639 struct crypto_comp *cc; /* crypto compressor stream */
640 atomic_t ready; /* ready to start flag */
641 atomic_t stop; /* ready to stop flag */
642 int ret; /* return code */
643 wait_queue_head_t go; /* start compression */
644 wait_queue_head_t done; /* compression done */
645 size_t unc_len; /* uncompressed length */
646 size_t cmp_len; /* compressed length */
647 unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
648 unsigned char cmp[CMP_SIZE]; /* compressed buffer */
651 /* Indicates the image size after compression */
652 static atomic_t compressed_size = ATOMIC_INIT(0);
655 * Compression function that runs in its own thread.
657 static int compress_threadfn(void *data)
659 struct cmp_data *d = data;
660 unsigned int cmp_len = 0;
663 wait_event(d->go, atomic_read_acquire(&d->ready) ||
664 kthread_should_stop());
665 if (kthread_should_stop()) {
668 atomic_set_release(&d->stop, 1);
672 atomic_set(&d->ready, 0);
674 cmp_len = CMP_SIZE - CMP_HEADER;
675 d->ret = crypto_comp_compress(d->cc, d->unc, d->unc_len,
678 d->cmp_len = cmp_len;
680 atomic_set(&compressed_size, atomic_read(&compressed_size) + d->cmp_len);
681 atomic_set_release(&d->stop, 1);
688 * save_compressed_image - Save the suspend image data after compression.
689 * @handle: Swap map handle to use for saving the image.
690 * @snapshot: Image to read data from.
691 * @nr_to_write: Number of pages to save.
693 static int save_compressed_image(struct swap_map_handle *handle,
694 struct snapshot_handle *snapshot,
695 unsigned int nr_to_write)
701 struct hib_bio_batch hb;
705 unsigned thr, run_threads, nr_threads;
706 unsigned char *page = NULL;
707 struct cmp_data *data = NULL;
708 struct crc_data *crc = NULL;
712 atomic_set(&compressed_size, 0);
715 * We'll limit the number of threads for compression to limit memory
718 nr_threads = num_online_cpus() - 1;
719 nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
721 page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
723 pr_err("Failed to allocate %s page\n", hib_comp_algo);
728 data = vzalloc(array_size(nr_threads, sizeof(*data)));
730 pr_err("Failed to allocate %s data\n", hib_comp_algo);
735 crc = kzalloc(sizeof(*crc), GFP_KERNEL);
737 pr_err("Failed to allocate crc\n");
743 * Start the compression threads.
745 for (thr = 0; thr < nr_threads; thr++) {
746 init_waitqueue_head(&data[thr].go);
747 init_waitqueue_head(&data[thr].done);
749 data[thr].cc = crypto_alloc_comp(hib_comp_algo, 0, 0);
750 if (IS_ERR_OR_NULL(data[thr].cc)) {
751 pr_err("Could not allocate comp stream %ld\n", PTR_ERR(data[thr].cc));
756 data[thr].thr = kthread_run(compress_threadfn,
758 "image_compress/%u", thr);
759 if (IS_ERR(data[thr].thr)) {
760 data[thr].thr = NULL;
761 pr_err("Cannot start compression threads\n");
768 * Start the CRC32 thread.
770 init_waitqueue_head(&crc->go);
771 init_waitqueue_head(&crc->done);
774 crc->crc32 = &handle->crc32;
775 for (thr = 0; thr < nr_threads; thr++) {
776 crc->unc[thr] = data[thr].unc;
777 crc->unc_len[thr] = &data[thr].unc_len;
780 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
781 if (IS_ERR(crc->thr)) {
783 pr_err("Cannot start CRC32 thread\n");
789 * Adjust the number of required free pages after all allocations have
790 * been done. We don't want to run out of pages when writing.
792 handle->reqd_free_pages = reqd_free_pages();
794 pr_info("Using %u thread(s) for %s compression\n", nr_threads, hib_comp_algo);
795 pr_info("Compressing and saving image data (%u pages)...\n",
797 m = nr_to_write / 10;
803 for (thr = 0; thr < nr_threads; thr++) {
804 for (off = 0; off < UNC_SIZE; off += PAGE_SIZE) {
805 ret = snapshot_read_next(snapshot);
812 memcpy(data[thr].unc + off,
813 data_of(*snapshot), PAGE_SIZE);
816 pr_info("Image saving progress: %3d%%\n",
823 data[thr].unc_len = off;
825 atomic_set_release(&data[thr].ready, 1);
826 wake_up(&data[thr].go);
832 crc->run_threads = thr;
833 atomic_set_release(&crc->ready, 1);
836 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
837 wait_event(data[thr].done,
838 atomic_read_acquire(&data[thr].stop));
839 atomic_set(&data[thr].stop, 0);
844 pr_err("%s compression failed\n", hib_comp_algo);
848 if (unlikely(!data[thr].cmp_len ||
850 bytes_worst_compress(data[thr].unc_len))) {
851 pr_err("Invalid %s compressed length\n", hib_comp_algo);
856 *(size_t *)data[thr].cmp = data[thr].cmp_len;
859 * Given we are writing one page at a time to disk, we
860 * copy that much from the buffer, although the last
861 * bit will likely be smaller than full page. This is
862 * OK - we saved the length of the compressed data, so
863 * any garbage at the end will be discarded when we
867 off < CMP_HEADER + data[thr].cmp_len;
869 memcpy(page, data[thr].cmp + off, PAGE_SIZE);
871 ret = swap_write_page(handle, page, &hb);
877 wait_event(crc->done, atomic_read_acquire(&crc->stop));
878 atomic_set(&crc->stop, 0);
882 err2 = hib_wait_io(&hb);
887 pr_info("Image saving done\n");
888 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
889 pr_info("Image size after compression: %d kbytes\n",
890 (atomic_read(&compressed_size) / 1024));
893 hib_finish_batch(&hb);
896 kthread_stop(crc->thr);
900 for (thr = 0; thr < nr_threads; thr++) {
902 kthread_stop(data[thr].thr);
904 crypto_free_comp(data[thr].cc);
908 if (page) free_page((unsigned long)page);
914 * enough_swap - Make sure we have enough swap to save the image.
916 * Returns TRUE or FALSE after checking the total amount of swap
917 * space available from the resume partition.
920 static int enough_swap(unsigned int nr_pages)
922 unsigned int free_swap = count_swap_pages(root_swap, 1);
923 unsigned int required;
925 pr_debug("Free swap pages: %u\n", free_swap);
927 required = PAGES_FOR_IO + nr_pages;
928 return free_swap > required;
932 * swsusp_write - Write entire image and metadata.
933 * @flags: flags to pass to the "boot" kernel in the image header
935 * It is important _NOT_ to umount filesystems at this point. We want
936 * them synced (in case something goes wrong) but we DO not want to mark
937 * filesystem clean: it is not. (And it does not matter, if we resume
938 * correctly, we'll mark system clean, anyway.)
941 int swsusp_write(unsigned int flags)
943 struct swap_map_handle handle;
944 struct snapshot_handle snapshot;
945 struct swsusp_info *header;
949 pages = snapshot_get_image_size();
950 error = get_swap_writer(&handle);
952 pr_err("Cannot get swap writer\n");
955 if (flags & SF_NOCOMPRESS_MODE) {
956 if (!enough_swap(pages)) {
957 pr_err("Not enough free swap\n");
962 memset(&snapshot, 0, sizeof(struct snapshot_handle));
963 error = snapshot_read_next(&snapshot);
964 if (error < (int)PAGE_SIZE) {
970 header = (struct swsusp_info *)data_of(snapshot);
971 error = swap_write_page(&handle, header, NULL);
973 error = (flags & SF_NOCOMPRESS_MODE) ?
974 save_image(&handle, &snapshot, pages - 1) :
975 save_compressed_image(&handle, &snapshot, pages - 1);
978 error = swap_writer_finish(&handle, flags, error);
983 * The following functions allow us to read data using a swap map
984 * in a file-like way.
987 static void release_swap_reader(struct swap_map_handle *handle)
989 struct swap_map_page_list *tmp;
991 while (handle->maps) {
992 if (handle->maps->map)
993 free_page((unsigned long)handle->maps->map);
995 handle->maps = handle->maps->next;
1001 static int get_swap_reader(struct swap_map_handle *handle,
1002 unsigned int *flags_p)
1005 struct swap_map_page_list *tmp, *last;
1008 *flags_p = swsusp_header->flags;
1010 if (!swsusp_header->image) /* how can this happen? */
1014 last = handle->maps = NULL;
1015 offset = swsusp_header->image;
1017 tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
1019 release_swap_reader(handle);
1028 tmp->map = (struct swap_map_page *)
1029 __get_free_page(GFP_NOIO | __GFP_HIGH);
1031 release_swap_reader(handle);
1035 error = hib_submit_io(REQ_OP_READ, offset, tmp->map, NULL);
1037 release_swap_reader(handle);
1040 offset = tmp->map->next_swap;
1043 handle->cur = handle->maps->map;
1047 static int swap_read_page(struct swap_map_handle *handle, void *buf,
1048 struct hib_bio_batch *hb)
1052 struct swap_map_page_list *tmp;
1056 offset = handle->cur->entries[handle->k];
1059 error = hib_submit_io(REQ_OP_READ, offset, buf, hb);
1062 if (++handle->k >= MAP_PAGE_ENTRIES) {
1064 free_page((unsigned long)handle->maps->map);
1066 handle->maps = handle->maps->next;
1069 release_swap_reader(handle);
1071 handle->cur = handle->maps->map;
1076 static int swap_reader_finish(struct swap_map_handle *handle)
1078 release_swap_reader(handle);
1084 * load_image - load the image using the swap map handle
1085 * @handle and the snapshot handle @snapshot
1086 * (assume there are @nr_pages pages to load)
1089 static int load_image(struct swap_map_handle *handle,
1090 struct snapshot_handle *snapshot,
1091 unsigned int nr_to_read)
1097 struct hib_bio_batch hb;
1101 hib_init_batch(&hb);
1103 clean_pages_on_read = true;
1104 pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
1105 m = nr_to_read / 10;
1109 start = ktime_get();
1111 ret = snapshot_write_next(snapshot);
1114 ret = swap_read_page(handle, data_of(*snapshot), &hb);
1117 if (snapshot->sync_read)
1118 ret = hib_wait_io(&hb);
1121 if (!(nr_pages % m))
1122 pr_info("Image loading progress: %3d%%\n",
1126 err2 = hib_wait_io(&hb);
1127 hib_finish_batch(&hb);
1132 pr_info("Image loading done\n");
1133 ret = snapshot_write_finalize(snapshot);
1134 if (!ret && !snapshot_image_loaded(snapshot))
1137 swsusp_show_speed(start, stop, nr_to_read, "Read");
1142 * Structure used for data decompression.
1145 struct task_struct *thr; /* thread */
1146 struct crypto_comp *cc; /* crypto compressor stream */
1147 atomic_t ready; /* ready to start flag */
1148 atomic_t stop; /* ready to stop flag */
1149 int ret; /* return code */
1150 wait_queue_head_t go; /* start decompression */
1151 wait_queue_head_t done; /* decompression done */
1152 size_t unc_len; /* uncompressed length */
1153 size_t cmp_len; /* compressed length */
1154 unsigned char unc[UNC_SIZE]; /* uncompressed buffer */
1155 unsigned char cmp[CMP_SIZE]; /* compressed buffer */
1159 * Decompression function that runs in its own thread.
1161 static int decompress_threadfn(void *data)
1163 struct dec_data *d = data;
1164 unsigned int unc_len = 0;
1167 wait_event(d->go, atomic_read_acquire(&d->ready) ||
1168 kthread_should_stop());
1169 if (kthread_should_stop()) {
1172 atomic_set_release(&d->stop, 1);
1176 atomic_set(&d->ready, 0);
1179 d->ret = crypto_comp_decompress(d->cc, d->cmp + CMP_HEADER, d->cmp_len,
1181 d->unc_len = unc_len;
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_comp(hib_comp_algo, 0, 0);
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].thr = kthread_run(decompress_threadfn,
1267 "image_decompress/%u", thr);
1268 if (IS_ERR(data[thr].thr)) {
1269 data[thr].thr = NULL;
1270 pr_err("Cannot start decompression threads\n");
1277 * Start the CRC32 thread.
1279 init_waitqueue_head(&crc->go);
1280 init_waitqueue_head(&crc->done);
1283 crc->crc32 = &handle->crc32;
1284 for (thr = 0; thr < nr_threads; thr++) {
1285 crc->unc[thr] = data[thr].unc;
1286 crc->unc_len[thr] = &data[thr].unc_len;
1289 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1290 if (IS_ERR(crc->thr)) {
1292 pr_err("Cannot start CRC32 thread\n");
1298 * Set the number of pages for read buffering.
1299 * This is complete guesswork, because we'll only know the real
1300 * picture once prepare_image() is called, which is much later on
1301 * during the image load phase. We'll assume the worst case and
1302 * say that none of the image pages are from high memory.
1304 if (low_free_pages() > snapshot_get_image_size())
1305 read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1306 read_pages = clamp_val(read_pages, CMP_MIN_RD_PAGES, CMP_MAX_RD_PAGES);
1308 for (i = 0; i < read_pages; i++) {
1309 page[i] = (void *)__get_free_page(i < CMP_PAGES ?
1310 GFP_NOIO | __GFP_HIGH :
1311 GFP_NOIO | __GFP_NOWARN |
1315 if (i < CMP_PAGES) {
1317 pr_err("Failed to allocate %s pages\n", hib_comp_algo);
1325 want = ring_size = i;
1327 pr_info("Using %u thread(s) for %s decompression\n", nr_threads, hib_comp_algo);
1328 pr_info("Loading and decompressing image data (%u pages)...\n",
1330 m = nr_to_read / 10;
1334 start = ktime_get();
1336 ret = snapshot_write_next(snapshot);
1341 for (i = 0; !eof && i < want; i++) {
1342 ret = swap_read_page(handle, page[ring], &hb);
1345 * On real read error, finish. On end of data,
1346 * set EOF flag and just exit the read loop.
1349 handle->cur->entries[handle->k]) {
1356 if (++ring >= ring_size)
1363 * We are out of data, wait for some more.
1369 ret = hib_wait_io(&hb);
1378 if (crc->run_threads) {
1379 wait_event(crc->done, atomic_read_acquire(&crc->stop));
1380 atomic_set(&crc->stop, 0);
1381 crc->run_threads = 0;
1384 for (thr = 0; have && thr < nr_threads; thr++) {
1385 data[thr].cmp_len = *(size_t *)page[pg];
1386 if (unlikely(!data[thr].cmp_len ||
1388 bytes_worst_compress(UNC_SIZE))) {
1389 pr_err("Invalid %s compressed length\n", hib_comp_algo);
1394 need = DIV_ROUND_UP(data[thr].cmp_len + CMP_HEADER,
1405 off < CMP_HEADER + data[thr].cmp_len;
1407 memcpy(data[thr].cmp + off,
1408 page[pg], PAGE_SIZE);
1411 if (++pg >= ring_size)
1415 atomic_set_release(&data[thr].ready, 1);
1416 wake_up(&data[thr].go);
1420 * Wait for more data while we are decompressing.
1422 if (have < CMP_PAGES && asked) {
1423 ret = hib_wait_io(&hb);
1432 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1433 wait_event(data[thr].done,
1434 atomic_read_acquire(&data[thr].stop));
1435 atomic_set(&data[thr].stop, 0);
1437 ret = data[thr].ret;
1440 pr_err("%s decompression failed\n", hib_comp_algo);
1444 if (unlikely(!data[thr].unc_len ||
1445 data[thr].unc_len > UNC_SIZE ||
1446 data[thr].unc_len & (PAGE_SIZE - 1))) {
1447 pr_err("Invalid %s uncompressed length\n", hib_comp_algo);
1453 off < data[thr].unc_len; off += PAGE_SIZE) {
1454 memcpy(data_of(*snapshot),
1455 data[thr].unc + off, PAGE_SIZE);
1457 if (!(nr_pages % m))
1458 pr_info("Image loading progress: %3d%%\n",
1462 ret = snapshot_write_next(snapshot);
1464 crc->run_threads = thr + 1;
1465 atomic_set_release(&crc->ready, 1);
1472 crc->run_threads = thr;
1473 atomic_set_release(&crc->ready, 1);
1478 if (crc->run_threads) {
1479 wait_event(crc->done, atomic_read_acquire(&crc->stop));
1480 atomic_set(&crc->stop, 0);
1484 pr_info("Image loading done\n");
1485 ret = snapshot_write_finalize(snapshot);
1486 if (!ret && !snapshot_image_loaded(snapshot))
1489 if (swsusp_header->flags & SF_CRC32_MODE) {
1490 if(handle->crc32 != swsusp_header->crc32) {
1491 pr_err("Invalid image CRC32!\n");
1497 swsusp_show_speed(start, stop, nr_to_read, "Read");
1499 hib_finish_batch(&hb);
1500 for (i = 0; i < ring_size; i++)
1501 free_page((unsigned long)page[i]);
1504 kthread_stop(crc->thr);
1508 for (thr = 0; thr < nr_threads; thr++) {
1510 kthread_stop(data[thr].thr);
1512 crypto_free_comp(data[thr].cc);
1522 * swsusp_read - read the hibernation image.
1523 * @flags_p: flags passed by the "frozen" kernel in the image header should
1524 * be written into this memory location
1527 int swsusp_read(unsigned int *flags_p)
1530 struct swap_map_handle handle;
1531 struct snapshot_handle snapshot;
1532 struct swsusp_info *header;
1534 memset(&snapshot, 0, sizeof(struct snapshot_handle));
1535 error = snapshot_write_next(&snapshot);
1536 if (error < (int)PAGE_SIZE)
1537 return error < 0 ? error : -EFAULT;
1538 header = (struct swsusp_info *)data_of(snapshot);
1539 error = get_swap_reader(&handle, flags_p);
1543 error = swap_read_page(&handle, header, NULL);
1545 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1546 load_image(&handle, &snapshot, header->pages - 1) :
1547 load_compressed_image(&handle, &snapshot, header->pages - 1);
1549 swap_reader_finish(&handle);
1552 pr_debug("Image successfully loaded\n");
1554 pr_debug("Error %d resuming\n", error);
1558 static void *swsusp_holder;
1561 * swsusp_check - Open the resume device and check for the swsusp signature.
1562 * @exclusive: Open the resume device exclusively.
1565 int swsusp_check(bool exclusive)
1567 void *holder = exclusive ? &swsusp_holder : NULL;
1570 hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
1571 BLK_OPEN_READ, holder, NULL);
1572 if (!IS_ERR(hib_resume_bdev_file)) {
1573 clear_page(swsusp_header);
1574 error = hib_submit_io(REQ_OP_READ, swsusp_resume_block,
1575 swsusp_header, NULL);
1579 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
1580 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
1581 swsusp_header_flags = swsusp_header->flags;
1582 /* Reset swap signature now */
1583 error = hib_submit_io(REQ_OP_WRITE | REQ_SYNC,
1584 swsusp_resume_block,
1585 swsusp_header, NULL);
1589 if (!error && swsusp_header->flags & SF_HW_SIG &&
1590 swsusp_header->hw_sig != swsusp_hardware_signature) {
1591 pr_info("Suspend image hardware signature mismatch (%08x now %08x); aborting resume.\n",
1592 swsusp_header->hw_sig, swsusp_hardware_signature);
1598 bdev_fput(hib_resume_bdev_file);
1600 pr_debug("Image signature found, resuming\n");
1602 error = PTR_ERR(hib_resume_bdev_file);
1606 pr_debug("Image not found (code %d)\n", error);
1612 * swsusp_close - close resume device.
1615 void swsusp_close(void)
1617 if (IS_ERR(hib_resume_bdev_file)) {
1618 pr_debug("Image device not initialised\n");
1622 fput(hib_resume_bdev_file);
1626 * swsusp_unmark - Unmark swsusp signature in the resume device
1629 #ifdef CONFIG_SUSPEND
1630 int swsusp_unmark(void)
1634 hib_submit_io(REQ_OP_READ, swsusp_resume_block,
1635 swsusp_header, NULL);
1636 if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1637 memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1638 error = hib_submit_io(REQ_OP_WRITE | REQ_SYNC,
1639 swsusp_resume_block,
1640 swsusp_header, NULL);
1642 pr_err("Cannot find swsusp signature!\n");
1647 * We just returned from suspend, we don't need the image any more.
1649 free_all_swap_pages(root_swap);
1655 static int __init swsusp_header_init(void)
1657 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1659 panic("Could not allocate memory for swsusp_header\n");
1663 core_initcall(swsusp_header_init);