2 * linux/kernel/power/swap.c
4 * This file provides functions for reading the suspend image from
5 * and writing it to a swap partition.
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
8 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
9 * Copyright (C) 2010 Bojan Smojver <bojan@rexursive.com>
11 * This file is released under the GPLv2.
15 #include <linux/module.h>
16 #include <linux/file.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/genhd.h>
20 #include <linux/device.h>
21 #include <linux/buffer_head.h>
22 #include <linux/bio.h>
23 #include <linux/blkdev.h>
24 #include <linux/swap.h>
25 #include <linux/swapops.h>
27 #include <linux/slab.h>
28 #include <linux/lzo.h>
29 #include <linux/vmalloc.h>
30 #include <linux/cpumask.h>
31 #include <linux/atomic.h>
32 #include <linux/kthread.h>
33 #include <linux/crc32.h>
37 #define HIBERNATE_SIG "S1SUSPEND"
40 * The swap map is a data structure used for keeping track of each page
41 * written to a swap partition. It consists of many swap_map_page
42 * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
43 * These structures are stored on the swap and linked together with the
44 * help of the .next_swap member.
46 * The swap map is created during suspend. The swap map pages are
47 * allocated and populated one at a time, so we only need one memory
48 * page to set up the entire structure.
50 * During resume we pick up all swap_map_page structures into a list.
53 #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
55 struct swap_map_page
{
56 sector_t entries
[MAP_PAGE_ENTRIES
];
60 struct swap_map_page_list
{
61 struct swap_map_page
*map
;
62 struct swap_map_page_list
*next
;
66 * The swap_map_handle structure is used for handling swap in
70 struct swap_map_handle
{
71 struct swap_map_page
*cur
;
72 struct swap_map_page_list
*maps
;
74 sector_t first_sector
;
76 unsigned long nr_free_pages
, written
;
80 struct swsusp_header
{
81 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
) - sizeof(int) -
85 unsigned int flags
; /* Flags to pass to the "boot" kernel */
88 } __attribute__((packed
));
90 static struct swsusp_header
*swsusp_header
;
93 * The following functions are used for tracing the allocated
94 * swap pages, so that they can be freed in case of an error.
97 struct swsusp_extent
{
103 static struct rb_root swsusp_extents
= RB_ROOT
;
105 static int swsusp_extents_insert(unsigned long swap_offset
)
107 struct rb_node
**new = &(swsusp_extents
.rb_node
);
108 struct rb_node
*parent
= NULL
;
109 struct swsusp_extent
*ext
;
111 /* Figure out where to put the new node */
113 ext
= container_of(*new, struct swsusp_extent
, node
);
115 if (swap_offset
< ext
->start
) {
117 if (swap_offset
== ext
->start
- 1) {
121 new = &((*new)->rb_left
);
122 } else if (swap_offset
> ext
->end
) {
124 if (swap_offset
== ext
->end
+ 1) {
128 new = &((*new)->rb_right
);
130 /* It already is in the tree */
134 /* Add the new node and rebalance the tree. */
135 ext
= kzalloc(sizeof(struct swsusp_extent
), GFP_KERNEL
);
139 ext
->start
= swap_offset
;
140 ext
->end
= swap_offset
;
141 rb_link_node(&ext
->node
, parent
, new);
142 rb_insert_color(&ext
->node
, &swsusp_extents
);
147 * alloc_swapdev_block - allocate a swap page and register that it has
148 * been allocated, so that it can be freed in case of an error.
151 sector_t
alloc_swapdev_block(int swap
)
153 unsigned long offset
;
155 offset
= swp_offset(get_swap_page_of_type(swap
));
157 if (swsusp_extents_insert(offset
))
158 swap_free(swp_entry(swap
, offset
));
160 return swapdev_block(swap
, offset
);
166 * free_all_swap_pages - free swap pages allocated for saving image data.
167 * It also frees the extents used to register which swap entries had been
171 void free_all_swap_pages(int swap
)
173 struct rb_node
*node
;
175 while ((node
= swsusp_extents
.rb_node
)) {
176 struct swsusp_extent
*ext
;
177 unsigned long offset
;
179 ext
= container_of(node
, struct swsusp_extent
, node
);
180 rb_erase(node
, &swsusp_extents
);
181 for (offset
= ext
->start
; offset
<= ext
->end
; offset
++)
182 swap_free(swp_entry(swap
, offset
));
188 int swsusp_swap_in_use(void)
190 return (swsusp_extents
.rb_node
!= NULL
);
197 static unsigned short root_swap
= 0xffff;
198 struct block_device
*hib_resume_bdev
;
204 static int mark_swapfiles(struct swap_map_handle
*handle
, unsigned int flags
)
208 hib_bio_read_page(swsusp_resume_block
, swsusp_header
, NULL
);
209 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
210 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
211 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
212 memcpy(swsusp_header
->sig
, HIBERNATE_SIG
, 10);
213 swsusp_header
->image
= handle
->first_sector
;
214 swsusp_header
->flags
= flags
;
215 if (flags
& SF_CRC32_MODE
)
216 swsusp_header
->crc32
= handle
->crc32
;
217 error
= hib_bio_write_page(swsusp_resume_block
,
218 swsusp_header
, NULL
);
220 printk(KERN_ERR
"PM: Swap header not found!\n");
227 * swsusp_swap_check - check if the resume device is a swap device
228 * and get its index (if so)
230 * This is called before saving image
232 static int swsusp_swap_check(void)
236 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
242 res
= blkdev_get(hib_resume_bdev
, FMODE_WRITE
, NULL
);
246 res
= set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
248 blkdev_put(hib_resume_bdev
, FMODE_WRITE
);
254 * write_page - Write one page to given swap location.
255 * @buf: Address we're writing.
256 * @offset: Offset of the swap page we're writing to.
257 * @bio_chain: Link the next write BIO here
260 static int write_page(void *buf
, sector_t offset
, struct bio
**bio_chain
)
269 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
273 ret
= hib_wait_on_bio_chain(bio_chain
); /* Free pages */
276 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
281 bio_chain
= NULL
; /* Go synchronous */
288 return hib_bio_write_page(offset
, src
, bio_chain
);
291 static void release_swap_writer(struct swap_map_handle
*handle
)
294 free_page((unsigned long)handle
->cur
);
298 static int get_swap_writer(struct swap_map_handle
*handle
)
302 ret
= swsusp_swap_check();
305 printk(KERN_ERR
"PM: Cannot find swap device, try "
309 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
314 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
315 if (!handle
->cur_swap
) {
320 handle
->nr_free_pages
= nr_free_pages() >> 1;
322 handle
->first_sector
= handle
->cur_swap
;
325 release_swap_writer(handle
);
327 swsusp_close(FMODE_WRITE
);
331 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
332 struct bio
**bio_chain
)
339 offset
= alloc_swapdev_block(root_swap
);
340 error
= write_page(buf
, offset
, bio_chain
);
343 handle
->cur
->entries
[handle
->k
++] = offset
;
344 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
345 offset
= alloc_swapdev_block(root_swap
);
348 handle
->cur
->next_swap
= offset
;
349 error
= write_page(handle
->cur
, handle
->cur_swap
, bio_chain
);
352 clear_page(handle
->cur
);
353 handle
->cur_swap
= offset
;
356 if (bio_chain
&& ++handle
->written
> handle
->nr_free_pages
) {
357 error
= hib_wait_on_bio_chain(bio_chain
);
366 static int flush_swap_writer(struct swap_map_handle
*handle
)
368 if (handle
->cur
&& handle
->cur_swap
)
369 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
374 static int swap_writer_finish(struct swap_map_handle
*handle
,
375 unsigned int flags
, int error
)
378 flush_swap_writer(handle
);
379 printk(KERN_INFO
"PM: S");
380 error
= mark_swapfiles(handle
, flags
);
385 free_all_swap_pages(root_swap
);
386 release_swap_writer(handle
);
387 swsusp_close(FMODE_WRITE
);
392 /* We need to remember how much compressed data we need to read. */
393 #define LZO_HEADER sizeof(size_t)
395 /* Number of pages/bytes we'll compress at one time. */
396 #define LZO_UNC_PAGES 32
397 #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
399 /* Number of pages/bytes we need for compressed data (worst case). */
400 #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
401 LZO_HEADER, PAGE_SIZE)
402 #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
404 /* Maximum number of threads for compression/decompression. */
405 #define LZO_THREADS 3
407 /* Maximum number of pages for read buffering. */
408 #define LZO_READ_PAGES (MAP_PAGE_ENTRIES * 8)
412 * save_image - save the suspend image data
415 static int save_image(struct swap_map_handle
*handle
,
416 struct snapshot_handle
*snapshot
,
417 unsigned int nr_to_write
)
424 struct timeval start
;
427 printk(KERN_INFO
"PM: Saving image data pages (%u pages) ... ",
429 m
= nr_to_write
/ 100;
434 do_gettimeofday(&start
);
436 ret
= snapshot_read_next(snapshot
);
439 ret
= swap_write_page(handle
, data_of(*snapshot
), &bio
);
443 printk(KERN_CONT
"\b\b\b\b%3d%%", nr_pages
/ m
);
446 err2
= hib_wait_on_bio_chain(&bio
);
447 do_gettimeofday(&stop
);
451 printk(KERN_CONT
"\b\b\b\bdone\n");
453 printk(KERN_CONT
"\n");
454 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
459 * Structure used for CRC32.
462 struct task_struct
*thr
; /* thread */
463 atomic_t ready
; /* ready to start flag */
464 atomic_t stop
; /* ready to stop flag */
465 unsigned run_threads
; /* nr current threads */
466 wait_queue_head_t go
; /* start crc update */
467 wait_queue_head_t done
; /* crc update done */
468 u32
*crc32
; /* points to handle's crc32 */
469 size_t *unc_len
[LZO_THREADS
]; /* uncompressed lengths */
470 unsigned char *unc
[LZO_THREADS
]; /* uncompressed data */
474 * CRC32 update function that runs in its own thread.
476 static int crc32_threadfn(void *data
)
478 struct crc_data
*d
= data
;
482 wait_event(d
->go
, atomic_read(&d
->ready
) ||
483 kthread_should_stop());
484 if (kthread_should_stop()) {
486 atomic_set(&d
->stop
, 1);
490 atomic_set(&d
->ready
, 0);
492 for (i
= 0; i
< d
->run_threads
; i
++)
493 *d
->crc32
= crc32_le(*d
->crc32
,
494 d
->unc
[i
], *d
->unc_len
[i
]);
495 atomic_set(&d
->stop
, 1);
501 * Structure used for LZO data compression.
504 struct task_struct
*thr
; /* thread */
505 atomic_t ready
; /* ready to start flag */
506 atomic_t stop
; /* ready to stop flag */
507 int ret
; /* return code */
508 wait_queue_head_t go
; /* start compression */
509 wait_queue_head_t done
; /* compression done */
510 size_t unc_len
; /* uncompressed length */
511 size_t cmp_len
; /* compressed length */
512 unsigned char unc
[LZO_UNC_SIZE
]; /* uncompressed buffer */
513 unsigned char cmp
[LZO_CMP_SIZE
]; /* compressed buffer */
514 unsigned char wrk
[LZO1X_1_MEM_COMPRESS
]; /* compression workspace */
518 * Compression function that runs in its own thread.
520 static int lzo_compress_threadfn(void *data
)
522 struct cmp_data
*d
= data
;
525 wait_event(d
->go
, atomic_read(&d
->ready
) ||
526 kthread_should_stop());
527 if (kthread_should_stop()) {
530 atomic_set(&d
->stop
, 1);
534 atomic_set(&d
->ready
, 0);
536 d
->ret
= lzo1x_1_compress(d
->unc
, d
->unc_len
,
537 d
->cmp
+ LZO_HEADER
, &d
->cmp_len
,
539 atomic_set(&d
->stop
, 1);
546 * save_image_lzo - Save the suspend image data compressed with LZO.
547 * @handle: Swap mam handle to use for saving the image.
548 * @snapshot: Image to read data from.
549 * @nr_to_write: Number of pages to save.
551 static int save_image_lzo(struct swap_map_handle
*handle
,
552 struct snapshot_handle
*snapshot
,
553 unsigned int nr_to_write
)
560 struct timeval start
;
563 unsigned thr
, run_threads
, nr_threads
;
564 unsigned char *page
= NULL
;
565 struct cmp_data
*data
= NULL
;
566 struct crc_data
*crc
= NULL
;
569 * We'll limit the number of threads for compression to limit memory
572 nr_threads
= num_online_cpus() - 1;
573 nr_threads
= clamp_val(nr_threads
, 1, LZO_THREADS
);
575 page
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
577 printk(KERN_ERR
"PM: Failed to allocate LZO page\n");
582 data
= vmalloc(sizeof(*data
) * nr_threads
);
584 printk(KERN_ERR
"PM: Failed to allocate LZO data\n");
588 for (thr
= 0; thr
< nr_threads
; thr
++)
589 memset(&data
[thr
], 0, offsetof(struct cmp_data
, go
));
591 crc
= kmalloc(sizeof(*crc
), GFP_KERNEL
);
593 printk(KERN_ERR
"PM: Failed to allocate crc\n");
597 memset(crc
, 0, offsetof(struct crc_data
, go
));
600 * Start the compression threads.
602 for (thr
= 0; thr
< nr_threads
; thr
++) {
603 init_waitqueue_head(&data
[thr
].go
);
604 init_waitqueue_head(&data
[thr
].done
);
606 data
[thr
].thr
= kthread_run(lzo_compress_threadfn
,
608 "image_compress/%u", thr
);
609 if (IS_ERR(data
[thr
].thr
)) {
610 data
[thr
].thr
= NULL
;
612 "PM: Cannot start compression threads\n");
619 * Adjust number of free pages after all allocations have been done.
620 * We don't want to run out of pages when writing.
622 handle
->nr_free_pages
= nr_free_pages() >> 1;
625 * Start the CRC32 thread.
627 init_waitqueue_head(&crc
->go
);
628 init_waitqueue_head(&crc
->done
);
631 crc
->crc32
= &handle
->crc32
;
632 for (thr
= 0; thr
< nr_threads
; thr
++) {
633 crc
->unc
[thr
] = data
[thr
].unc
;
634 crc
->unc_len
[thr
] = &data
[thr
].unc_len
;
637 crc
->thr
= kthread_run(crc32_threadfn
, crc
, "image_crc32");
638 if (IS_ERR(crc
->thr
)) {
640 printk(KERN_ERR
"PM: Cannot start CRC32 thread\n");
646 "PM: Using %u thread(s) for compression.\n"
647 "PM: Compressing and saving image data (%u pages) ... ",
648 nr_threads
, nr_to_write
);
649 m
= nr_to_write
/ 100;
654 do_gettimeofday(&start
);
656 for (thr
= 0; thr
< nr_threads
; thr
++) {
657 for (off
= 0; off
< LZO_UNC_SIZE
; off
+= PAGE_SIZE
) {
658 ret
= snapshot_read_next(snapshot
);
665 memcpy(data
[thr
].unc
+ off
,
666 data_of(*snapshot
), PAGE_SIZE
);
669 printk(KERN_CONT
"\b\b\b\b%3d%%",
676 data
[thr
].unc_len
= off
;
678 atomic_set(&data
[thr
].ready
, 1);
679 wake_up(&data
[thr
].go
);
685 crc
->run_threads
= thr
;
686 atomic_set(&crc
->ready
, 1);
689 for (run_threads
= thr
, thr
= 0; thr
< run_threads
; thr
++) {
690 wait_event(data
[thr
].done
,
691 atomic_read(&data
[thr
].stop
));
692 atomic_set(&data
[thr
].stop
, 0);
697 printk(KERN_ERR
"PM: LZO compression failed\n");
701 if (unlikely(!data
[thr
].cmp_len
||
703 lzo1x_worst_compress(data
[thr
].unc_len
))) {
705 "PM: Invalid LZO compressed length\n");
710 *(size_t *)data
[thr
].cmp
= data
[thr
].cmp_len
;
713 * Given we are writing one page at a time to disk, we
714 * copy that much from the buffer, although the last
715 * bit will likely be smaller than full page. This is
716 * OK - we saved the length of the compressed data, so
717 * any garbage at the end will be discarded when we
721 off
< LZO_HEADER
+ data
[thr
].cmp_len
;
723 memcpy(page
, data
[thr
].cmp
+ off
, PAGE_SIZE
);
725 ret
= swap_write_page(handle
, page
, &bio
);
731 wait_event(crc
->done
, atomic_read(&crc
->stop
));
732 atomic_set(&crc
->stop
, 0);
736 err2
= hib_wait_on_bio_chain(&bio
);
737 do_gettimeofday(&stop
);
741 printk(KERN_CONT
"\b\b\b\bdone\n");
743 printk(KERN_CONT
"\n");
745 swsusp_show_speed(&start
, &stop
, nr_to_write
, "Wrote");
749 kthread_stop(crc
->thr
);
753 for (thr
= 0; thr
< nr_threads
; thr
++)
755 kthread_stop(data
[thr
].thr
);
758 if (page
) free_page((unsigned long)page
);
764 * enough_swap - Make sure we have enough swap to save the image.
766 * Returns TRUE or FALSE after checking the total amount of swap
767 * space avaiable from the resume partition.
770 static int enough_swap(unsigned int nr_pages
, unsigned int flags
)
772 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
773 unsigned int required
;
775 pr_debug("PM: Free swap pages: %u\n", free_swap
);
777 required
= PAGES_FOR_IO
+ ((flags
& SF_NOCOMPRESS_MODE
) ?
778 nr_pages
: (nr_pages
* LZO_CMP_PAGES
) / LZO_UNC_PAGES
+ 1);
779 return free_swap
> required
;
783 * swsusp_write - Write entire image and metadata.
784 * @flags: flags to pass to the "boot" kernel in the image header
786 * It is important _NOT_ to umount filesystems at this point. We want
787 * them synced (in case something goes wrong) but we DO not want to mark
788 * filesystem clean: it is not. (And it does not matter, if we resume
789 * correctly, we'll mark system clean, anyway.)
792 int swsusp_write(unsigned int flags
)
794 struct swap_map_handle handle
;
795 struct snapshot_handle snapshot
;
796 struct swsusp_info
*header
;
800 pages
= snapshot_get_image_size();
801 error
= get_swap_writer(&handle
);
803 printk(KERN_ERR
"PM: Cannot get swap writer\n");
806 if (!enough_swap(pages
, flags
)) {
807 printk(KERN_ERR
"PM: Not enough free swap\n");
811 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
812 error
= snapshot_read_next(&snapshot
);
813 if (error
< PAGE_SIZE
) {
819 header
= (struct swsusp_info
*)data_of(snapshot
);
820 error
= swap_write_page(&handle
, header
, NULL
);
822 error
= (flags
& SF_NOCOMPRESS_MODE
) ?
823 save_image(&handle
, &snapshot
, pages
- 1) :
824 save_image_lzo(&handle
, &snapshot
, pages
- 1);
827 error
= swap_writer_finish(&handle
, flags
, error
);
832 * The following functions allow us to read data using a swap map
833 * in a file-alike way
836 static void release_swap_reader(struct swap_map_handle
*handle
)
838 struct swap_map_page_list
*tmp
;
840 while (handle
->maps
) {
841 if (handle
->maps
->map
)
842 free_page((unsigned long)handle
->maps
->map
);
844 handle
->maps
= handle
->maps
->next
;
850 static int get_swap_reader(struct swap_map_handle
*handle
,
851 unsigned int *flags_p
)
854 struct swap_map_page_list
*tmp
, *last
;
857 *flags_p
= swsusp_header
->flags
;
859 if (!swsusp_header
->image
) /* how can this happen? */
863 last
= handle
->maps
= NULL
;
864 offset
= swsusp_header
->image
;
866 tmp
= kmalloc(sizeof(*handle
->maps
), GFP_KERNEL
);
868 release_swap_reader(handle
);
871 memset(tmp
, 0, sizeof(*tmp
));
878 tmp
->map
= (struct swap_map_page
*)
879 __get_free_page(__GFP_WAIT
| __GFP_HIGH
);
881 release_swap_reader(handle
);
885 error
= hib_bio_read_page(offset
, tmp
->map
, NULL
);
887 release_swap_reader(handle
);
890 offset
= tmp
->map
->next_swap
;
893 handle
->cur
= handle
->maps
->map
;
897 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
898 struct bio
**bio_chain
)
902 struct swap_map_page_list
*tmp
;
906 offset
= handle
->cur
->entries
[handle
->k
];
909 error
= hib_bio_read_page(offset
, buf
, bio_chain
);
912 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
914 free_page((unsigned long)handle
->maps
->map
);
916 handle
->maps
= handle
->maps
->next
;
919 release_swap_reader(handle
);
921 handle
->cur
= handle
->maps
->map
;
926 static int swap_reader_finish(struct swap_map_handle
*handle
)
928 release_swap_reader(handle
);
934 * load_image - load the image using the swap map handle
935 * @handle and the snapshot handle @snapshot
936 * (assume there are @nr_pages pages to load)
939 static int load_image(struct swap_map_handle
*handle
,
940 struct snapshot_handle
*snapshot
,
941 unsigned int nr_to_read
)
945 struct timeval start
;
951 printk(KERN_INFO
"PM: Loading image data pages (%u pages) ... ",
953 m
= nr_to_read
/ 100;
958 do_gettimeofday(&start
);
960 ret
= snapshot_write_next(snapshot
);
963 ret
= swap_read_page(handle
, data_of(*snapshot
), &bio
);
966 if (snapshot
->sync_read
)
967 ret
= hib_wait_on_bio_chain(&bio
);
971 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
974 err2
= hib_wait_on_bio_chain(&bio
);
975 do_gettimeofday(&stop
);
979 printk("\b\b\b\bdone\n");
980 snapshot_write_finalize(snapshot
);
981 if (!snapshot_image_loaded(snapshot
))
985 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
990 * Structure used for LZO data decompression.
993 struct task_struct
*thr
; /* thread */
994 atomic_t ready
; /* ready to start flag */
995 atomic_t stop
; /* ready to stop flag */
996 int ret
; /* return code */
997 wait_queue_head_t go
; /* start decompression */
998 wait_queue_head_t done
; /* decompression done */
999 size_t unc_len
; /* uncompressed length */
1000 size_t cmp_len
; /* compressed length */
1001 unsigned char unc
[LZO_UNC_SIZE
]; /* uncompressed buffer */
1002 unsigned char cmp
[LZO_CMP_SIZE
]; /* compressed buffer */
1006 * Deompression function that runs in its own thread.
1008 static int lzo_decompress_threadfn(void *data
)
1010 struct dec_data
*d
= data
;
1013 wait_event(d
->go
, atomic_read(&d
->ready
) ||
1014 kthread_should_stop());
1015 if (kthread_should_stop()) {
1018 atomic_set(&d
->stop
, 1);
1022 atomic_set(&d
->ready
, 0);
1024 d
->unc_len
= LZO_UNC_SIZE
;
1025 d
->ret
= lzo1x_decompress_safe(d
->cmp
+ LZO_HEADER
, d
->cmp_len
,
1026 d
->unc
, &d
->unc_len
);
1027 atomic_set(&d
->stop
, 1);
1034 * load_image_lzo - Load compressed image data and decompress them with LZO.
1035 * @handle: Swap map handle to use for loading data.
1036 * @snapshot: Image to copy uncompressed data into.
1037 * @nr_to_read: Number of pages to load.
1039 static int load_image_lzo(struct swap_map_handle
*handle
,
1040 struct snapshot_handle
*snapshot
,
1041 unsigned int nr_to_read
)
1047 struct timeval start
;
1048 struct timeval stop
;
1051 unsigned i
, thr
, run_threads
, nr_threads
;
1052 unsigned ring
= 0, pg
= 0, ring_size
= 0,
1053 have
= 0, want
, need
, asked
= 0;
1054 unsigned long read_pages
;
1055 unsigned char **page
= NULL
;
1056 struct dec_data
*data
= NULL
;
1057 struct crc_data
*crc
= NULL
;
1060 * We'll limit the number of threads for decompression to limit memory
1063 nr_threads
= num_online_cpus() - 1;
1064 nr_threads
= clamp_val(nr_threads
, 1, LZO_THREADS
);
1066 page
= vmalloc(sizeof(*page
) * LZO_READ_PAGES
);
1068 printk(KERN_ERR
"PM: Failed to allocate LZO page\n");
1073 data
= vmalloc(sizeof(*data
) * nr_threads
);
1075 printk(KERN_ERR
"PM: Failed to allocate LZO data\n");
1079 for (thr
= 0; thr
< nr_threads
; thr
++)
1080 memset(&data
[thr
], 0, offsetof(struct dec_data
, go
));
1082 crc
= kmalloc(sizeof(*crc
), GFP_KERNEL
);
1084 printk(KERN_ERR
"PM: Failed to allocate crc\n");
1088 memset(crc
, 0, offsetof(struct crc_data
, go
));
1091 * Start the decompression threads.
1093 for (thr
= 0; thr
< nr_threads
; thr
++) {
1094 init_waitqueue_head(&data
[thr
].go
);
1095 init_waitqueue_head(&data
[thr
].done
);
1097 data
[thr
].thr
= kthread_run(lzo_decompress_threadfn
,
1099 "image_decompress/%u", thr
);
1100 if (IS_ERR(data
[thr
].thr
)) {
1101 data
[thr
].thr
= NULL
;
1103 "PM: Cannot start decompression threads\n");
1110 * Start the CRC32 thread.
1112 init_waitqueue_head(&crc
->go
);
1113 init_waitqueue_head(&crc
->done
);
1116 crc
->crc32
= &handle
->crc32
;
1117 for (thr
= 0; thr
< nr_threads
; thr
++) {
1118 crc
->unc
[thr
] = data
[thr
].unc
;
1119 crc
->unc_len
[thr
] = &data
[thr
].unc_len
;
1122 crc
->thr
= kthread_run(crc32_threadfn
, crc
, "image_crc32");
1123 if (IS_ERR(crc
->thr
)) {
1125 printk(KERN_ERR
"PM: Cannot start CRC32 thread\n");
1131 * Adjust number of pages for read buffering, in case we are short.
1133 read_pages
= (nr_free_pages() - snapshot_get_image_size()) >> 1;
1134 read_pages
= clamp_val(read_pages
, LZO_CMP_PAGES
, LZO_READ_PAGES
);
1136 for (i
= 0; i
< read_pages
; i
++) {
1137 page
[i
] = (void *)__get_free_page(i
< LZO_CMP_PAGES
?
1138 __GFP_WAIT
| __GFP_HIGH
:
1141 if (i
< LZO_CMP_PAGES
) {
1144 "PM: Failed to allocate LZO pages\n");
1152 want
= ring_size
= i
;
1155 "PM: Using %u thread(s) for decompression.\n"
1156 "PM: Loading and decompressing image data (%u pages) ... ",
1157 nr_threads
, nr_to_read
);
1158 m
= nr_to_read
/ 100;
1163 do_gettimeofday(&start
);
1165 ret
= snapshot_write_next(snapshot
);
1170 for (i
= 0; !eof
&& i
< want
; i
++) {
1171 ret
= swap_read_page(handle
, page
[ring
], &bio
);
1174 * On real read error, finish. On end of data,
1175 * set EOF flag and just exit the read loop.
1178 handle
->cur
->entries
[handle
->k
]) {
1185 if (++ring
>= ring_size
)
1192 * We are out of data, wait for some more.
1198 ret
= hib_wait_on_bio_chain(&bio
);
1207 if (crc
->run_threads
) {
1208 wait_event(crc
->done
, atomic_read(&crc
->stop
));
1209 atomic_set(&crc
->stop
, 0);
1210 crc
->run_threads
= 0;
1213 for (thr
= 0; have
&& thr
< nr_threads
; thr
++) {
1214 data
[thr
].cmp_len
= *(size_t *)page
[pg
];
1215 if (unlikely(!data
[thr
].cmp_len
||
1217 lzo1x_worst_compress(LZO_UNC_SIZE
))) {
1219 "PM: Invalid LZO compressed length\n");
1224 need
= DIV_ROUND_UP(data
[thr
].cmp_len
+ LZO_HEADER
,
1235 off
< LZO_HEADER
+ data
[thr
].cmp_len
;
1237 memcpy(data
[thr
].cmp
+ off
,
1238 page
[pg
], PAGE_SIZE
);
1241 if (++pg
>= ring_size
)
1245 atomic_set(&data
[thr
].ready
, 1);
1246 wake_up(&data
[thr
].go
);
1250 * Wait for more data while we are decompressing.
1252 if (have
< LZO_CMP_PAGES
&& asked
) {
1253 ret
= hib_wait_on_bio_chain(&bio
);
1262 for (run_threads
= thr
, thr
= 0; thr
< run_threads
; thr
++) {
1263 wait_event(data
[thr
].done
,
1264 atomic_read(&data
[thr
].stop
));
1265 atomic_set(&data
[thr
].stop
, 0);
1267 ret
= data
[thr
].ret
;
1271 "PM: LZO decompression failed\n");
1275 if (unlikely(!data
[thr
].unc_len
||
1276 data
[thr
].unc_len
> LZO_UNC_SIZE
||
1277 data
[thr
].unc_len
& (PAGE_SIZE
- 1))) {
1279 "PM: Invalid LZO uncompressed length\n");
1285 off
< data
[thr
].unc_len
; off
+= PAGE_SIZE
) {
1286 memcpy(data_of(*snapshot
),
1287 data
[thr
].unc
+ off
, PAGE_SIZE
);
1289 if (!(nr_pages
% m
))
1290 printk("\b\b\b\b%3d%%", nr_pages
/ m
);
1293 ret
= snapshot_write_next(snapshot
);
1295 crc
->run_threads
= thr
+ 1;
1296 atomic_set(&crc
->ready
, 1);
1303 crc
->run_threads
= thr
;
1304 atomic_set(&crc
->ready
, 1);
1309 if (crc
->run_threads
) {
1310 wait_event(crc
->done
, atomic_read(&crc
->stop
));
1311 atomic_set(&crc
->stop
, 0);
1313 do_gettimeofday(&stop
);
1315 printk("\b\b\b\bdone\n");
1316 snapshot_write_finalize(snapshot
);
1317 if (!snapshot_image_loaded(snapshot
))
1320 if (swsusp_header
->flags
& SF_CRC32_MODE
) {
1321 if(handle
->crc32
!= swsusp_header
->crc32
) {
1323 "PM: Invalid image CRC32!\n");
1330 swsusp_show_speed(&start
, &stop
, nr_to_read
, "Read");
1332 for (i
= 0; i
< ring_size
; i
++)
1333 free_page((unsigned long)page
[i
]);
1336 kthread_stop(crc
->thr
);
1340 for (thr
= 0; thr
< nr_threads
; thr
++)
1342 kthread_stop(data
[thr
].thr
);
1345 if (page
) vfree(page
);
1351 * swsusp_read - read the hibernation image.
1352 * @flags_p: flags passed by the "frozen" kernel in the image header should
1353 * be written into this memory location
1356 int swsusp_read(unsigned int *flags_p
)
1359 struct swap_map_handle handle
;
1360 struct snapshot_handle snapshot
;
1361 struct swsusp_info
*header
;
1363 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
1364 error
= snapshot_write_next(&snapshot
);
1365 if (error
< PAGE_SIZE
)
1366 return error
< 0 ? error
: -EFAULT
;
1367 header
= (struct swsusp_info
*)data_of(snapshot
);
1368 error
= get_swap_reader(&handle
, flags_p
);
1372 error
= swap_read_page(&handle
, header
, NULL
);
1374 error
= (*flags_p
& SF_NOCOMPRESS_MODE
) ?
1375 load_image(&handle
, &snapshot
, header
->pages
- 1) :
1376 load_image_lzo(&handle
, &snapshot
, header
->pages
- 1);
1378 swap_reader_finish(&handle
);
1381 pr_debug("PM: Image successfully loaded\n");
1383 pr_debug("PM: Error %d resuming\n", error
);
1388 * swsusp_check - Check for swsusp signature in the resume device
1391 int swsusp_check(void)
1395 hib_resume_bdev
= blkdev_get_by_dev(swsusp_resume_device
,
1397 if (!IS_ERR(hib_resume_bdev
)) {
1398 set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
1399 clear_page(swsusp_header
);
1400 error
= hib_bio_read_page(swsusp_resume_block
,
1401 swsusp_header
, NULL
);
1405 if (!memcmp(HIBERNATE_SIG
, swsusp_header
->sig
, 10)) {
1406 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
1407 /* Reset swap signature now */
1408 error
= hib_bio_write_page(swsusp_resume_block
,
1409 swsusp_header
, NULL
);
1416 blkdev_put(hib_resume_bdev
, FMODE_READ
);
1418 pr_debug("PM: Image signature found, resuming\n");
1420 error
= PTR_ERR(hib_resume_bdev
);
1424 pr_debug("PM: Image not found (code %d)\n", error
);
1430 * swsusp_close - close swap device.
1433 void swsusp_close(fmode_t mode
)
1435 if (IS_ERR(hib_resume_bdev
)) {
1436 pr_debug("PM: Image device not initialised\n");
1440 blkdev_put(hib_resume_bdev
, mode
);
1443 static int swsusp_header_init(void)
1445 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
1447 panic("Could not allocate memory for swsusp_header\n");
1451 core_initcall(swsusp_header_init
);