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-2012 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/bio.h>
22 #include <linux/blkdev.h>
23 #include <linux/swap.h>
24 #include <linux/swapops.h>
26 #include <linux/slab.h>
27 #include <linux/lzo.h>
28 #include <linux/vmalloc.h>
29 #include <linux/cpumask.h>
30 #include <linux/atomic.h>
31 #include <linux/kthread.h>
32 #include <linux/crc32.h>
33 #include <linux/ktime.h>
37 #define HIBERNATE_SIG "S1SUSPEND"
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)
56 * Number of free pages that are not high.
58 static inline unsigned long low_free_pages(void)
60 return nr_free_pages() - nr_free_highpages();
64 * Number of pages required to be kept free while writing the image. Always
65 * half of all available low pages before the writing starts.
67 static inline unsigned long reqd_free_pages(void)
69 return low_free_pages() / 2;
72 struct swap_map_page
{
73 sector_t entries
[MAP_PAGE_ENTRIES
];
77 struct swap_map_page_list
{
78 struct swap_map_page
*map
;
79 struct swap_map_page_list
*next
;
83 * The swap_map_handle structure is used for handling swap in
87 struct swap_map_handle
{
88 struct swap_map_page
*cur
;
89 struct swap_map_page_list
*maps
;
91 sector_t first_sector
;
93 unsigned long reqd_free_pages
;
97 struct swsusp_header
{
98 char reserved
[PAGE_SIZE
- 20 - sizeof(sector_t
) - sizeof(int) -
102 unsigned int flags
; /* Flags to pass to the "boot" kernel */
107 static struct swsusp_header
*swsusp_header
;
110 * The following functions are used for tracing the allocated
111 * swap pages, so that they can be freed in case of an error.
114 struct swsusp_extent
{
120 static struct rb_root swsusp_extents
= RB_ROOT
;
122 static int swsusp_extents_insert(unsigned long swap_offset
)
124 struct rb_node
**new = &(swsusp_extents
.rb_node
);
125 struct rb_node
*parent
= NULL
;
126 struct swsusp_extent
*ext
;
128 /* Figure out where to put the new node */
130 ext
= rb_entry(*new, struct swsusp_extent
, node
);
132 if (swap_offset
< ext
->start
) {
134 if (swap_offset
== ext
->start
- 1) {
138 new = &((*new)->rb_left
);
139 } else if (swap_offset
> ext
->end
) {
141 if (swap_offset
== ext
->end
+ 1) {
145 new = &((*new)->rb_right
);
147 /* It already is in the tree */
151 /* Add the new node and rebalance the tree. */
152 ext
= kzalloc(sizeof(struct swsusp_extent
), GFP_KERNEL
);
156 ext
->start
= swap_offset
;
157 ext
->end
= swap_offset
;
158 rb_link_node(&ext
->node
, parent
, new);
159 rb_insert_color(&ext
->node
, &swsusp_extents
);
164 * alloc_swapdev_block - allocate a swap page and register that it has
165 * been allocated, so that it can be freed in case of an error.
168 sector_t
alloc_swapdev_block(int swap
)
170 unsigned long offset
;
172 offset
= swp_offset(get_swap_page_of_type(swap
));
174 if (swsusp_extents_insert(offset
))
175 swap_free(swp_entry(swap
, offset
));
177 return swapdev_block(swap
, offset
);
183 * free_all_swap_pages - free swap pages allocated for saving image data.
184 * It also frees the extents used to register which swap entries had been
188 void free_all_swap_pages(int swap
)
190 struct rb_node
*node
;
192 while ((node
= swsusp_extents
.rb_node
)) {
193 struct swsusp_extent
*ext
;
194 unsigned long offset
;
196 ext
= container_of(node
, struct swsusp_extent
, node
);
197 rb_erase(node
, &swsusp_extents
);
198 for (offset
= ext
->start
; offset
<= ext
->end
; offset
++)
199 swap_free(swp_entry(swap
, offset
));
205 int swsusp_swap_in_use(void)
207 return (swsusp_extents
.rb_node
!= NULL
);
214 static unsigned short root_swap
= 0xffff;
215 static struct block_device
*hib_resume_bdev
;
217 struct hib_bio_batch
{
219 wait_queue_head_t wait
;
223 static void hib_init_batch(struct hib_bio_batch
*hb
)
225 atomic_set(&hb
->count
, 0);
226 init_waitqueue_head(&hb
->wait
);
230 static void hib_end_io(struct bio
*bio
, int error
)
232 struct hib_bio_batch
*hb
= bio
->bi_private
;
233 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
234 struct page
*page
= bio
->bi_io_vec
[0].bv_page
;
236 if (!uptodate
|| error
) {
237 printk(KERN_ALERT
"Read-error on swap-device (%u:%u:%Lu)\n",
238 imajor(bio
->bi_bdev
->bd_inode
),
239 iminor(bio
->bi_bdev
->bd_inode
),
240 (unsigned long long)bio
->bi_iter
.bi_sector
);
246 if (bio_data_dir(bio
) == WRITE
)
249 if (error
&& !hb
->error
)
251 if (atomic_dec_and_test(&hb
->count
))
257 static int hib_submit_io(int rw
, pgoff_t page_off
, void *addr
,
258 struct hib_bio_batch
*hb
)
260 struct page
*page
= virt_to_page(addr
);
264 bio
= bio_alloc(__GFP_WAIT
| __GFP_HIGH
, 1);
265 bio
->bi_iter
.bi_sector
= page_off
* (PAGE_SIZE
>> 9);
266 bio
->bi_bdev
= hib_resume_bdev
;
268 if (bio_add_page(bio
, page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
269 printk(KERN_ERR
"PM: Adding page to bio failed at %llu\n",
270 (unsigned long long)bio
->bi_iter
.bi_sector
);
276 bio
->bi_end_io
= hib_end_io
;
277 bio
->bi_private
= hb
;
278 atomic_inc(&hb
->count
);
281 error
= submit_bio_wait(rw
, bio
);
288 static int hib_wait_io(struct hib_bio_batch
*hb
)
290 wait_event(hb
->wait
, atomic_read(&hb
->count
) == 0);
298 static int mark_swapfiles(struct swap_map_handle
*handle
, unsigned int flags
)
302 hib_submit_io(READ_SYNC
, swsusp_resume_block
, swsusp_header
, NULL
);
303 if (!memcmp("SWAP-SPACE",swsusp_header
->sig
, 10) ||
304 !memcmp("SWAPSPACE2",swsusp_header
->sig
, 10)) {
305 memcpy(swsusp_header
->orig_sig
,swsusp_header
->sig
, 10);
306 memcpy(swsusp_header
->sig
, HIBERNATE_SIG
, 10);
307 swsusp_header
->image
= handle
->first_sector
;
308 swsusp_header
->flags
= flags
;
309 if (flags
& SF_CRC32_MODE
)
310 swsusp_header
->crc32
= handle
->crc32
;
311 error
= hib_submit_io(WRITE_SYNC
, swsusp_resume_block
,
312 swsusp_header
, NULL
);
314 printk(KERN_ERR
"PM: Swap header not found!\n");
321 * swsusp_swap_check - check if the resume device is a swap device
322 * and get its index (if so)
324 * This is called before saving image
326 static int swsusp_swap_check(void)
330 res
= swap_type_of(swsusp_resume_device
, swsusp_resume_block
,
336 res
= blkdev_get(hib_resume_bdev
, FMODE_WRITE
, NULL
);
340 res
= set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
342 blkdev_put(hib_resume_bdev
, FMODE_WRITE
);
348 * write_page - Write one page to given swap location.
349 * @buf: Address we're writing.
350 * @offset: Offset of the swap page we're writing to.
351 * @hb: bio completion batch
354 static int write_page(void *buf
, sector_t offset
, struct hib_bio_batch
*hb
)
363 src
= (void *)__get_free_page(__GFP_WAIT
| __GFP_NOWARN
|
368 ret
= hib_wait_io(hb
); /* Free pages */
371 src
= (void *)__get_free_page(__GFP_WAIT
|
378 hb
= NULL
; /* Go synchronous */
385 return hib_submit_io(WRITE_SYNC
, offset
, src
, hb
);
388 static void release_swap_writer(struct swap_map_handle
*handle
)
391 free_page((unsigned long)handle
->cur
);
395 static int get_swap_writer(struct swap_map_handle
*handle
)
399 ret
= swsusp_swap_check();
402 printk(KERN_ERR
"PM: Cannot find swap device, try "
406 handle
->cur
= (struct swap_map_page
*)get_zeroed_page(GFP_KERNEL
);
411 handle
->cur_swap
= alloc_swapdev_block(root_swap
);
412 if (!handle
->cur_swap
) {
417 handle
->reqd_free_pages
= reqd_free_pages();
418 handle
->first_sector
= handle
->cur_swap
;
421 release_swap_writer(handle
);
423 swsusp_close(FMODE_WRITE
);
427 static int swap_write_page(struct swap_map_handle
*handle
, void *buf
,
428 struct hib_bio_batch
*hb
)
435 offset
= alloc_swapdev_block(root_swap
);
436 error
= write_page(buf
, offset
, hb
);
439 handle
->cur
->entries
[handle
->k
++] = offset
;
440 if (handle
->k
>= MAP_PAGE_ENTRIES
) {
441 offset
= alloc_swapdev_block(root_swap
);
444 handle
->cur
->next_swap
= offset
;
445 error
= write_page(handle
->cur
, handle
->cur_swap
, hb
);
448 clear_page(handle
->cur
);
449 handle
->cur_swap
= offset
;
452 if (hb
&& low_free_pages() <= handle
->reqd_free_pages
) {
453 error
= hib_wait_io(hb
);
457 * Recalculate the number of required free pages, to
458 * make sure we never take more than half.
460 handle
->reqd_free_pages
= reqd_free_pages();
467 static int flush_swap_writer(struct swap_map_handle
*handle
)
469 if (handle
->cur
&& handle
->cur_swap
)
470 return write_page(handle
->cur
, handle
->cur_swap
, NULL
);
475 static int swap_writer_finish(struct swap_map_handle
*handle
,
476 unsigned int flags
, int error
)
479 flush_swap_writer(handle
);
480 printk(KERN_INFO
"PM: S");
481 error
= mark_swapfiles(handle
, flags
);
486 free_all_swap_pages(root_swap
);
487 release_swap_writer(handle
);
488 swsusp_close(FMODE_WRITE
);
493 /* We need to remember how much compressed data we need to read. */
494 #define LZO_HEADER sizeof(size_t)
496 /* Number of pages/bytes we'll compress at one time. */
497 #define LZO_UNC_PAGES 32
498 #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
500 /* Number of pages/bytes we need for compressed data (worst case). */
501 #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
502 LZO_HEADER, PAGE_SIZE)
503 #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
505 /* Maximum number of threads for compression/decompression. */
506 #define LZO_THREADS 3
508 /* Minimum/maximum number of pages for read buffering. */
509 #define LZO_MIN_RD_PAGES 1024
510 #define LZO_MAX_RD_PAGES 8192
514 * save_image - save the suspend image data
517 static int save_image(struct swap_map_handle
*handle
,
518 struct snapshot_handle
*snapshot
,
519 unsigned int nr_to_write
)
525 struct hib_bio_batch hb
;
531 printk(KERN_INFO
"PM: Saving image data pages (%u pages)...\n",
533 m
= nr_to_write
/ 10;
539 ret
= snapshot_read_next(snapshot
);
542 ret
= swap_write_page(handle
, data_of(*snapshot
), &hb
);
546 printk(KERN_INFO
"PM: Image saving progress: %3d%%\n",
550 err2
= hib_wait_io(&hb
);
555 printk(KERN_INFO
"PM: Image saving done.\n");
556 swsusp_show_speed(start
, stop
, nr_to_write
, "Wrote");
561 * Structure used for CRC32.
564 struct task_struct
*thr
; /* thread */
565 atomic_t ready
; /* ready to start flag */
566 atomic_t stop
; /* ready to stop flag */
567 unsigned run_threads
; /* nr current threads */
568 wait_queue_head_t go
; /* start crc update */
569 wait_queue_head_t done
; /* crc update done */
570 u32
*crc32
; /* points to handle's crc32 */
571 size_t *unc_len
[LZO_THREADS
]; /* uncompressed lengths */
572 unsigned char *unc
[LZO_THREADS
]; /* uncompressed data */
576 * CRC32 update function that runs in its own thread.
578 static int crc32_threadfn(void *data
)
580 struct crc_data
*d
= data
;
584 wait_event(d
->go
, atomic_read(&d
->ready
) ||
585 kthread_should_stop());
586 if (kthread_should_stop()) {
588 atomic_set(&d
->stop
, 1);
592 atomic_set(&d
->ready
, 0);
594 for (i
= 0; i
< d
->run_threads
; i
++)
595 *d
->crc32
= crc32_le(*d
->crc32
,
596 d
->unc
[i
], *d
->unc_len
[i
]);
597 atomic_set(&d
->stop
, 1);
603 * Structure used for LZO data compression.
606 struct task_struct
*thr
; /* thread */
607 atomic_t ready
; /* ready to start flag */
608 atomic_t stop
; /* ready to stop flag */
609 int ret
; /* return code */
610 wait_queue_head_t go
; /* start compression */
611 wait_queue_head_t done
; /* compression done */
612 size_t unc_len
; /* uncompressed length */
613 size_t cmp_len
; /* compressed length */
614 unsigned char unc
[LZO_UNC_SIZE
]; /* uncompressed buffer */
615 unsigned char cmp
[LZO_CMP_SIZE
]; /* compressed buffer */
616 unsigned char wrk
[LZO1X_1_MEM_COMPRESS
]; /* compression workspace */
620 * Compression function that runs in its own thread.
622 static int lzo_compress_threadfn(void *data
)
624 struct cmp_data
*d
= data
;
627 wait_event(d
->go
, atomic_read(&d
->ready
) ||
628 kthread_should_stop());
629 if (kthread_should_stop()) {
632 atomic_set(&d
->stop
, 1);
636 atomic_set(&d
->ready
, 0);
638 d
->ret
= lzo1x_1_compress(d
->unc
, d
->unc_len
,
639 d
->cmp
+ LZO_HEADER
, &d
->cmp_len
,
641 atomic_set(&d
->stop
, 1);
648 * save_image_lzo - Save the suspend image data compressed with LZO.
649 * @handle: Swap map handle to use for saving the image.
650 * @snapshot: Image to read data from.
651 * @nr_to_write: Number of pages to save.
653 static int save_image_lzo(struct swap_map_handle
*handle
,
654 struct snapshot_handle
*snapshot
,
655 unsigned int nr_to_write
)
661 struct hib_bio_batch hb
;
665 unsigned thr
, run_threads
, nr_threads
;
666 unsigned char *page
= NULL
;
667 struct cmp_data
*data
= NULL
;
668 struct crc_data
*crc
= NULL
;
673 * We'll limit the number of threads for compression to limit memory
676 nr_threads
= num_online_cpus() - 1;
677 nr_threads
= clamp_val(nr_threads
, 1, LZO_THREADS
);
679 page
= (void *)__get_free_page(__GFP_WAIT
| __GFP_HIGH
);
681 printk(KERN_ERR
"PM: Failed to allocate LZO page\n");
686 data
= vmalloc(sizeof(*data
) * nr_threads
);
688 printk(KERN_ERR
"PM: Failed to allocate LZO data\n");
692 for (thr
= 0; thr
< nr_threads
; thr
++)
693 memset(&data
[thr
], 0, offsetof(struct cmp_data
, go
));
695 crc
= kmalloc(sizeof(*crc
), GFP_KERNEL
);
697 printk(KERN_ERR
"PM: Failed to allocate crc\n");
701 memset(crc
, 0, offsetof(struct crc_data
, go
));
704 * Start the compression threads.
706 for (thr
= 0; thr
< nr_threads
; thr
++) {
707 init_waitqueue_head(&data
[thr
].go
);
708 init_waitqueue_head(&data
[thr
].done
);
710 data
[thr
].thr
= kthread_run(lzo_compress_threadfn
,
712 "image_compress/%u", thr
);
713 if (IS_ERR(data
[thr
].thr
)) {
714 data
[thr
].thr
= NULL
;
716 "PM: Cannot start compression threads\n");
723 * Start the CRC32 thread.
725 init_waitqueue_head(&crc
->go
);
726 init_waitqueue_head(&crc
->done
);
729 crc
->crc32
= &handle
->crc32
;
730 for (thr
= 0; thr
< nr_threads
; thr
++) {
731 crc
->unc
[thr
] = data
[thr
].unc
;
732 crc
->unc_len
[thr
] = &data
[thr
].unc_len
;
735 crc
->thr
= kthread_run(crc32_threadfn
, crc
, "image_crc32");
736 if (IS_ERR(crc
->thr
)) {
738 printk(KERN_ERR
"PM: Cannot start CRC32 thread\n");
744 * Adjust the number of required free pages after all allocations have
745 * been done. We don't want to run out of pages when writing.
747 handle
->reqd_free_pages
= reqd_free_pages();
750 "PM: Using %u thread(s) for compression.\n"
751 "PM: Compressing and saving image data (%u pages)...\n",
752 nr_threads
, nr_to_write
);
753 m
= nr_to_write
/ 10;
759 for (thr
= 0; thr
< nr_threads
; thr
++) {
760 for (off
= 0; off
< LZO_UNC_SIZE
; off
+= PAGE_SIZE
) {
761 ret
= snapshot_read_next(snapshot
);
768 memcpy(data
[thr
].unc
+ off
,
769 data_of(*snapshot
), PAGE_SIZE
);
773 "PM: Image saving progress: "
781 data
[thr
].unc_len
= off
;
783 atomic_set(&data
[thr
].ready
, 1);
784 wake_up(&data
[thr
].go
);
790 crc
->run_threads
= thr
;
791 atomic_set(&crc
->ready
, 1);
794 for (run_threads
= thr
, thr
= 0; thr
< run_threads
; thr
++) {
795 wait_event(data
[thr
].done
,
796 atomic_read(&data
[thr
].stop
));
797 atomic_set(&data
[thr
].stop
, 0);
802 printk(KERN_ERR
"PM: LZO compression failed\n");
806 if (unlikely(!data
[thr
].cmp_len
||
808 lzo1x_worst_compress(data
[thr
].unc_len
))) {
810 "PM: Invalid LZO compressed length\n");
815 *(size_t *)data
[thr
].cmp
= data
[thr
].cmp_len
;
818 * Given we are writing one page at a time to disk, we
819 * copy that much from the buffer, although the last
820 * bit will likely be smaller than full page. This is
821 * OK - we saved the length of the compressed data, so
822 * any garbage at the end will be discarded when we
826 off
< LZO_HEADER
+ data
[thr
].cmp_len
;
828 memcpy(page
, data
[thr
].cmp
+ off
, PAGE_SIZE
);
830 ret
= swap_write_page(handle
, page
, &hb
);
836 wait_event(crc
->done
, atomic_read(&crc
->stop
));
837 atomic_set(&crc
->stop
, 0);
841 err2
= hib_wait_io(&hb
);
846 printk(KERN_INFO
"PM: Image saving done.\n");
847 swsusp_show_speed(start
, stop
, nr_to_write
, "Wrote");
851 kthread_stop(crc
->thr
);
855 for (thr
= 0; thr
< nr_threads
; thr
++)
857 kthread_stop(data
[thr
].thr
);
860 if (page
) free_page((unsigned long)page
);
866 * enough_swap - Make sure we have enough swap to save the image.
868 * Returns TRUE or FALSE after checking the total amount of swap
869 * space avaiable from the resume partition.
872 static int enough_swap(unsigned int nr_pages
, unsigned int flags
)
874 unsigned int free_swap
= count_swap_pages(root_swap
, 1);
875 unsigned int required
;
877 pr_debug("PM: Free swap pages: %u\n", free_swap
);
879 required
= PAGES_FOR_IO
+ nr_pages
;
880 return free_swap
> required
;
884 * swsusp_write - Write entire image and metadata.
885 * @flags: flags to pass to the "boot" kernel in the image header
887 * It is important _NOT_ to umount filesystems at this point. We want
888 * them synced (in case something goes wrong) but we DO not want to mark
889 * filesystem clean: it is not. (And it does not matter, if we resume
890 * correctly, we'll mark system clean, anyway.)
893 int swsusp_write(unsigned int flags
)
895 struct swap_map_handle handle
;
896 struct snapshot_handle snapshot
;
897 struct swsusp_info
*header
;
901 pages
= snapshot_get_image_size();
902 error
= get_swap_writer(&handle
);
904 printk(KERN_ERR
"PM: Cannot get swap writer\n");
907 if (flags
& SF_NOCOMPRESS_MODE
) {
908 if (!enough_swap(pages
, flags
)) {
909 printk(KERN_ERR
"PM: Not enough free swap\n");
914 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
915 error
= snapshot_read_next(&snapshot
);
916 if (error
< PAGE_SIZE
) {
922 header
= (struct swsusp_info
*)data_of(snapshot
);
923 error
= swap_write_page(&handle
, header
, NULL
);
925 error
= (flags
& SF_NOCOMPRESS_MODE
) ?
926 save_image(&handle
, &snapshot
, pages
- 1) :
927 save_image_lzo(&handle
, &snapshot
, pages
- 1);
930 error
= swap_writer_finish(&handle
, flags
, error
);
935 * The following functions allow us to read data using a swap map
936 * in a file-alike way
939 static void release_swap_reader(struct swap_map_handle
*handle
)
941 struct swap_map_page_list
*tmp
;
943 while (handle
->maps
) {
944 if (handle
->maps
->map
)
945 free_page((unsigned long)handle
->maps
->map
);
947 handle
->maps
= handle
->maps
->next
;
953 static int get_swap_reader(struct swap_map_handle
*handle
,
954 unsigned int *flags_p
)
957 struct swap_map_page_list
*tmp
, *last
;
960 *flags_p
= swsusp_header
->flags
;
962 if (!swsusp_header
->image
) /* how can this happen? */
966 last
= handle
->maps
= NULL
;
967 offset
= swsusp_header
->image
;
969 tmp
= kmalloc(sizeof(*handle
->maps
), GFP_KERNEL
);
971 release_swap_reader(handle
);
974 memset(tmp
, 0, sizeof(*tmp
));
981 tmp
->map
= (struct swap_map_page
*)
982 __get_free_page(__GFP_WAIT
| __GFP_HIGH
);
984 release_swap_reader(handle
);
988 error
= hib_submit_io(READ_SYNC
, offset
, tmp
->map
, NULL
);
990 release_swap_reader(handle
);
993 offset
= tmp
->map
->next_swap
;
996 handle
->cur
= handle
->maps
->map
;
1000 static int swap_read_page(struct swap_map_handle
*handle
, void *buf
,
1001 struct hib_bio_batch
*hb
)
1005 struct swap_map_page_list
*tmp
;
1009 offset
= handle
->cur
->entries
[handle
->k
];
1012 error
= hib_submit_io(READ_SYNC
, offset
, buf
, hb
);
1015 if (++handle
->k
>= MAP_PAGE_ENTRIES
) {
1017 free_page((unsigned long)handle
->maps
->map
);
1019 handle
->maps
= handle
->maps
->next
;
1022 release_swap_reader(handle
);
1024 handle
->cur
= handle
->maps
->map
;
1029 static int swap_reader_finish(struct swap_map_handle
*handle
)
1031 release_swap_reader(handle
);
1037 * load_image - load the image using the swap map handle
1038 * @handle and the snapshot handle @snapshot
1039 * (assume there are @nr_pages pages to load)
1042 static int load_image(struct swap_map_handle
*handle
,
1043 struct snapshot_handle
*snapshot
,
1044 unsigned int nr_to_read
)
1050 struct hib_bio_batch hb
;
1054 hib_init_batch(&hb
);
1056 printk(KERN_INFO
"PM: Loading image data pages (%u pages)...\n",
1058 m
= nr_to_read
/ 10;
1062 start
= ktime_get();
1064 ret
= snapshot_write_next(snapshot
);
1067 ret
= swap_read_page(handle
, data_of(*snapshot
), &hb
);
1070 if (snapshot
->sync_read
)
1071 ret
= hib_wait_io(&hb
);
1074 if (!(nr_pages
% m
))
1075 printk(KERN_INFO
"PM: Image loading progress: %3d%%\n",
1079 err2
= hib_wait_io(&hb
);
1084 printk(KERN_INFO
"PM: Image loading done.\n");
1085 snapshot_write_finalize(snapshot
);
1086 if (!snapshot_image_loaded(snapshot
))
1089 swsusp_show_speed(start
, stop
, nr_to_read
, "Read");
1094 * Structure used for LZO data decompression.
1097 struct task_struct
*thr
; /* thread */
1098 atomic_t ready
; /* ready to start flag */
1099 atomic_t stop
; /* ready to stop flag */
1100 int ret
; /* return code */
1101 wait_queue_head_t go
; /* start decompression */
1102 wait_queue_head_t done
; /* decompression done */
1103 size_t unc_len
; /* uncompressed length */
1104 size_t cmp_len
; /* compressed length */
1105 unsigned char unc
[LZO_UNC_SIZE
]; /* uncompressed buffer */
1106 unsigned char cmp
[LZO_CMP_SIZE
]; /* compressed buffer */
1110 * Deompression function that runs in its own thread.
1112 static int lzo_decompress_threadfn(void *data
)
1114 struct dec_data
*d
= data
;
1117 wait_event(d
->go
, atomic_read(&d
->ready
) ||
1118 kthread_should_stop());
1119 if (kthread_should_stop()) {
1122 atomic_set(&d
->stop
, 1);
1126 atomic_set(&d
->ready
, 0);
1128 d
->unc_len
= LZO_UNC_SIZE
;
1129 d
->ret
= lzo1x_decompress_safe(d
->cmp
+ LZO_HEADER
, d
->cmp_len
,
1130 d
->unc
, &d
->unc_len
);
1131 atomic_set(&d
->stop
, 1);
1138 * load_image_lzo - Load compressed image data and decompress them with LZO.
1139 * @handle: Swap map handle to use for loading data.
1140 * @snapshot: Image to copy uncompressed data into.
1141 * @nr_to_read: Number of pages to load.
1143 static int load_image_lzo(struct swap_map_handle
*handle
,
1144 struct snapshot_handle
*snapshot
,
1145 unsigned int nr_to_read
)
1150 struct hib_bio_batch hb
;
1155 unsigned i
, thr
, run_threads
, nr_threads
;
1156 unsigned ring
= 0, pg
= 0, ring_size
= 0,
1157 have
= 0, want
, need
, asked
= 0;
1158 unsigned long read_pages
= 0;
1159 unsigned char **page
= NULL
;
1160 struct dec_data
*data
= NULL
;
1161 struct crc_data
*crc
= NULL
;
1163 hib_init_batch(&hb
);
1166 * We'll limit the number of threads for decompression to limit memory
1169 nr_threads
= num_online_cpus() - 1;
1170 nr_threads
= clamp_val(nr_threads
, 1, LZO_THREADS
);
1172 page
= vmalloc(sizeof(*page
) * LZO_MAX_RD_PAGES
);
1174 printk(KERN_ERR
"PM: Failed to allocate LZO page\n");
1179 data
= vmalloc(sizeof(*data
) * nr_threads
);
1181 printk(KERN_ERR
"PM: Failed to allocate LZO data\n");
1185 for (thr
= 0; thr
< nr_threads
; thr
++)
1186 memset(&data
[thr
], 0, offsetof(struct dec_data
, go
));
1188 crc
= kmalloc(sizeof(*crc
), GFP_KERNEL
);
1190 printk(KERN_ERR
"PM: Failed to allocate crc\n");
1194 memset(crc
, 0, offsetof(struct crc_data
, go
));
1197 * Start the decompression threads.
1199 for (thr
= 0; thr
< nr_threads
; thr
++) {
1200 init_waitqueue_head(&data
[thr
].go
);
1201 init_waitqueue_head(&data
[thr
].done
);
1203 data
[thr
].thr
= kthread_run(lzo_decompress_threadfn
,
1205 "image_decompress/%u", thr
);
1206 if (IS_ERR(data
[thr
].thr
)) {
1207 data
[thr
].thr
= NULL
;
1209 "PM: Cannot start decompression threads\n");
1216 * Start the CRC32 thread.
1218 init_waitqueue_head(&crc
->go
);
1219 init_waitqueue_head(&crc
->done
);
1222 crc
->crc32
= &handle
->crc32
;
1223 for (thr
= 0; thr
< nr_threads
; thr
++) {
1224 crc
->unc
[thr
] = data
[thr
].unc
;
1225 crc
->unc_len
[thr
] = &data
[thr
].unc_len
;
1228 crc
->thr
= kthread_run(crc32_threadfn
, crc
, "image_crc32");
1229 if (IS_ERR(crc
->thr
)) {
1231 printk(KERN_ERR
"PM: Cannot start CRC32 thread\n");
1237 * Set the number of pages for read buffering.
1238 * This is complete guesswork, because we'll only know the real
1239 * picture once prepare_image() is called, which is much later on
1240 * during the image load phase. We'll assume the worst case and
1241 * say that none of the image pages are from high memory.
1243 if (low_free_pages() > snapshot_get_image_size())
1244 read_pages
= (low_free_pages() - snapshot_get_image_size()) / 2;
1245 read_pages
= clamp_val(read_pages
, LZO_MIN_RD_PAGES
, LZO_MAX_RD_PAGES
);
1247 for (i
= 0; i
< read_pages
; i
++) {
1248 page
[i
] = (void *)__get_free_page(i
< LZO_CMP_PAGES
?
1249 __GFP_WAIT
| __GFP_HIGH
:
1250 __GFP_WAIT
| __GFP_NOWARN
|
1254 if (i
< LZO_CMP_PAGES
) {
1257 "PM: Failed to allocate LZO pages\n");
1265 want
= ring_size
= i
;
1268 "PM: Using %u thread(s) for decompression.\n"
1269 "PM: Loading and decompressing image data (%u pages)...\n",
1270 nr_threads
, nr_to_read
);
1271 m
= nr_to_read
/ 10;
1275 start
= ktime_get();
1277 ret
= snapshot_write_next(snapshot
);
1282 for (i
= 0; !eof
&& i
< want
; i
++) {
1283 ret
= swap_read_page(handle
, page
[ring
], &hb
);
1286 * On real read error, finish. On end of data,
1287 * set EOF flag and just exit the read loop.
1290 handle
->cur
->entries
[handle
->k
]) {
1297 if (++ring
>= ring_size
)
1304 * We are out of data, wait for some more.
1310 ret
= hib_wait_io(&hb
);
1319 if (crc
->run_threads
) {
1320 wait_event(crc
->done
, atomic_read(&crc
->stop
));
1321 atomic_set(&crc
->stop
, 0);
1322 crc
->run_threads
= 0;
1325 for (thr
= 0; have
&& thr
< nr_threads
; thr
++) {
1326 data
[thr
].cmp_len
= *(size_t *)page
[pg
];
1327 if (unlikely(!data
[thr
].cmp_len
||
1329 lzo1x_worst_compress(LZO_UNC_SIZE
))) {
1331 "PM: Invalid LZO compressed length\n");
1336 need
= DIV_ROUND_UP(data
[thr
].cmp_len
+ LZO_HEADER
,
1347 off
< LZO_HEADER
+ data
[thr
].cmp_len
;
1349 memcpy(data
[thr
].cmp
+ off
,
1350 page
[pg
], PAGE_SIZE
);
1353 if (++pg
>= ring_size
)
1357 atomic_set(&data
[thr
].ready
, 1);
1358 wake_up(&data
[thr
].go
);
1362 * Wait for more data while we are decompressing.
1364 if (have
< LZO_CMP_PAGES
&& asked
) {
1365 ret
= hib_wait_io(&hb
);
1374 for (run_threads
= thr
, thr
= 0; thr
< run_threads
; thr
++) {
1375 wait_event(data
[thr
].done
,
1376 atomic_read(&data
[thr
].stop
));
1377 atomic_set(&data
[thr
].stop
, 0);
1379 ret
= data
[thr
].ret
;
1383 "PM: LZO decompression failed\n");
1387 if (unlikely(!data
[thr
].unc_len
||
1388 data
[thr
].unc_len
> LZO_UNC_SIZE
||
1389 data
[thr
].unc_len
& (PAGE_SIZE
- 1))) {
1391 "PM: Invalid LZO uncompressed length\n");
1397 off
< data
[thr
].unc_len
; off
+= PAGE_SIZE
) {
1398 memcpy(data_of(*snapshot
),
1399 data
[thr
].unc
+ off
, PAGE_SIZE
);
1401 if (!(nr_pages
% m
))
1403 "PM: Image loading progress: "
1408 ret
= snapshot_write_next(snapshot
);
1410 crc
->run_threads
= thr
+ 1;
1411 atomic_set(&crc
->ready
, 1);
1418 crc
->run_threads
= thr
;
1419 atomic_set(&crc
->ready
, 1);
1424 if (crc
->run_threads
) {
1425 wait_event(crc
->done
, atomic_read(&crc
->stop
));
1426 atomic_set(&crc
->stop
, 0);
1430 printk(KERN_INFO
"PM: Image loading done.\n");
1431 snapshot_write_finalize(snapshot
);
1432 if (!snapshot_image_loaded(snapshot
))
1435 if (swsusp_header
->flags
& SF_CRC32_MODE
) {
1436 if(handle
->crc32
!= swsusp_header
->crc32
) {
1438 "PM: Invalid image CRC32!\n");
1444 swsusp_show_speed(start
, stop
, nr_to_read
, "Read");
1446 for (i
= 0; i
< ring_size
; i
++)
1447 free_page((unsigned long)page
[i
]);
1450 kthread_stop(crc
->thr
);
1454 for (thr
= 0; thr
< nr_threads
; thr
++)
1456 kthread_stop(data
[thr
].thr
);
1465 * swsusp_read - read the hibernation image.
1466 * @flags_p: flags passed by the "frozen" kernel in the image header should
1467 * be written into this memory location
1470 int swsusp_read(unsigned int *flags_p
)
1473 struct swap_map_handle handle
;
1474 struct snapshot_handle snapshot
;
1475 struct swsusp_info
*header
;
1477 memset(&snapshot
, 0, sizeof(struct snapshot_handle
));
1478 error
= snapshot_write_next(&snapshot
);
1479 if (error
< PAGE_SIZE
)
1480 return error
< 0 ? error
: -EFAULT
;
1481 header
= (struct swsusp_info
*)data_of(snapshot
);
1482 error
= get_swap_reader(&handle
, flags_p
);
1486 error
= swap_read_page(&handle
, header
, NULL
);
1488 error
= (*flags_p
& SF_NOCOMPRESS_MODE
) ?
1489 load_image(&handle
, &snapshot
, header
->pages
- 1) :
1490 load_image_lzo(&handle
, &snapshot
, header
->pages
- 1);
1492 swap_reader_finish(&handle
);
1495 pr_debug("PM: Image successfully loaded\n");
1497 pr_debug("PM: Error %d resuming\n", error
);
1502 * swsusp_check - Check for swsusp signature in the resume device
1505 int swsusp_check(void)
1509 hib_resume_bdev
= blkdev_get_by_dev(swsusp_resume_device
,
1511 if (!IS_ERR(hib_resume_bdev
)) {
1512 set_blocksize(hib_resume_bdev
, PAGE_SIZE
);
1513 clear_page(swsusp_header
);
1514 error
= hib_submit_io(READ_SYNC
, swsusp_resume_block
,
1515 swsusp_header
, NULL
);
1519 if (!memcmp(HIBERNATE_SIG
, swsusp_header
->sig
, 10)) {
1520 memcpy(swsusp_header
->sig
, swsusp_header
->orig_sig
, 10);
1521 /* Reset swap signature now */
1522 error
= hib_submit_io(WRITE_SYNC
, swsusp_resume_block
,
1523 swsusp_header
, NULL
);
1530 blkdev_put(hib_resume_bdev
, FMODE_READ
);
1532 pr_debug("PM: Image signature found, resuming\n");
1534 error
= PTR_ERR(hib_resume_bdev
);
1538 pr_debug("PM: Image not found (code %d)\n", error
);
1544 * swsusp_close - close swap device.
1547 void swsusp_close(fmode_t mode
)
1549 if (IS_ERR(hib_resume_bdev
)) {
1550 pr_debug("PM: Image device not initialised\n");
1554 blkdev_put(hib_resume_bdev
, mode
);
1558 * swsusp_unmark - Unmark swsusp signature in the resume device
1561 #ifdef CONFIG_SUSPEND
1562 int swsusp_unmark(void)
1566 hib_submit_io(READ_SYNC
, swsusp_resume_block
, swsusp_header
, NULL
);
1567 if (!memcmp(HIBERNATE_SIG
,swsusp_header
->sig
, 10)) {
1568 memcpy(swsusp_header
->sig
,swsusp_header
->orig_sig
, 10);
1569 error
= hib_submit_io(WRITE_SYNC
, swsusp_resume_block
,
1570 swsusp_header
, NULL
);
1572 printk(KERN_ERR
"PM: Cannot find swsusp signature!\n");
1577 * We just returned from suspend, we don't need the image any more.
1579 free_all_swap_pages(root_swap
);
1585 static int swsusp_header_init(void)
1587 swsusp_header
= (struct swsusp_header
*) __get_free_page(GFP_KERNEL
);
1589 panic("Could not allocate memory for swsusp_header\n");
1593 core_initcall(swsusp_header_init
);