]>
Commit | Line | Data |
---|---|---|
61159a31 RW |
1 | /* |
2 | * linux/kernel/power/swap.c | |
3 | * | |
4 | * This file provides functions for reading the suspend image from | |
5 | * and writing it to a swap partition. | |
6 | * | |
a2531293 | 7 | * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz> |
61159a31 | 8 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> |
5a21d489 | 9 | * Copyright (C) 2010-2012 Bojan Smojver <bojan@rexursive.com> |
61159a31 RW |
10 | * |
11 | * This file is released under the GPLv2. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <linux/module.h> | |
61159a31 | 16 | #include <linux/file.h> |
61159a31 RW |
17 | #include <linux/delay.h> |
18 | #include <linux/bitops.h> | |
19 | #include <linux/genhd.h> | |
20 | #include <linux/device.h> | |
61159a31 | 21 | #include <linux/bio.h> |
546e0d27 | 22 | #include <linux/blkdev.h> |
61159a31 RW |
23 | #include <linux/swap.h> |
24 | #include <linux/swapops.h> | |
25 | #include <linux/pm.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
f996fc96 BS |
27 | #include <linux/lzo.h> |
28 | #include <linux/vmalloc.h> | |
081a9d04 BS |
29 | #include <linux/cpumask.h> |
30 | #include <linux/atomic.h> | |
31 | #include <linux/kthread.h> | |
32 | #include <linux/crc32.h> | |
db597605 | 33 | #include <linux/ktime.h> |
61159a31 RW |
34 | |
35 | #include "power.h" | |
36 | ||
be8cd644 | 37 | #define HIBERNATE_SIG "S1SUSPEND" |
61159a31 | 38 | |
f6cf0545 JM |
39 | /* |
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. | |
43 | */ | |
44 | static bool clean_pages_on_read; | |
45 | static bool clean_pages_on_decompress; | |
46 | ||
51fb352b JS |
47 | /* |
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 | |
90133673 | 50 | * structures that contain each an array of MAP_PAGE_ENTRIES swap entries. |
51fb352b JS |
51 | * These structures are stored on the swap and linked together with the |
52 | * help of the .next_swap member. | |
53 | * | |
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. | |
57 | * | |
081a9d04 | 58 | * During resume we pick up all swap_map_page structures into a list. |
51fb352b JS |
59 | */ |
60 | ||
61 | #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1) | |
62 | ||
f8262d47 BS |
63 | /* |
64 | * Number of free pages that are not high. | |
65 | */ | |
66 | static inline unsigned long low_free_pages(void) | |
67 | { | |
68 | return nr_free_pages() - nr_free_highpages(); | |
69 | } | |
70 | ||
71 | /* | |
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. | |
74 | */ | |
75 | static inline unsigned long reqd_free_pages(void) | |
76 | { | |
77 | return low_free_pages() / 2; | |
78 | } | |
79 | ||
51fb352b JS |
80 | struct swap_map_page { |
81 | sector_t entries[MAP_PAGE_ENTRIES]; | |
82 | sector_t next_swap; | |
83 | }; | |
84 | ||
081a9d04 BS |
85 | struct swap_map_page_list { |
86 | struct swap_map_page *map; | |
87 | struct swap_map_page_list *next; | |
88 | }; | |
89 | ||
51fb352b JS |
90 | /** |
91 | * The swap_map_handle structure is used for handling swap in | |
92 | * a file-alike way | |
93 | */ | |
94 | ||
95 | struct swap_map_handle { | |
96 | struct swap_map_page *cur; | |
081a9d04 | 97 | struct swap_map_page_list *maps; |
51fb352b JS |
98 | sector_t cur_swap; |
99 | sector_t first_sector; | |
100 | unsigned int k; | |
f8262d47 | 101 | unsigned long reqd_free_pages; |
081a9d04 | 102 | u32 crc32; |
51fb352b JS |
103 | }; |
104 | ||
1b29c164 | 105 | struct swsusp_header { |
081a9d04 BS |
106 | char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) - |
107 | sizeof(u32)]; | |
108 | u32 crc32; | |
3aef83e0 | 109 | sector_t image; |
a634cc10 | 110 | unsigned int flags; /* Flags to pass to the "boot" kernel */ |
61159a31 RW |
111 | char orig_sig[10]; |
112 | char sig[10]; | |
52f5684c | 113 | } __packed; |
1b29c164 VG |
114 | |
115 | static struct swsusp_header *swsusp_header; | |
61159a31 | 116 | |
0414f2ec NC |
117 | /** |
118 | * The following functions are used for tracing the allocated | |
119 | * swap pages, so that they can be freed in case of an error. | |
120 | */ | |
121 | ||
122 | struct swsusp_extent { | |
123 | struct rb_node node; | |
124 | unsigned long start; | |
125 | unsigned long end; | |
126 | }; | |
127 | ||
128 | static struct rb_root swsusp_extents = RB_ROOT; | |
129 | ||
130 | static int swsusp_extents_insert(unsigned long swap_offset) | |
131 | { | |
132 | struct rb_node **new = &(swsusp_extents.rb_node); | |
133 | struct rb_node *parent = NULL; | |
134 | struct swsusp_extent *ext; | |
135 | ||
136 | /* Figure out where to put the new node */ | |
137 | while (*new) { | |
8316bd72 | 138 | ext = rb_entry(*new, struct swsusp_extent, node); |
0414f2ec NC |
139 | parent = *new; |
140 | if (swap_offset < ext->start) { | |
141 | /* Try to merge */ | |
142 | if (swap_offset == ext->start - 1) { | |
143 | ext->start--; | |
144 | return 0; | |
145 | } | |
146 | new = &((*new)->rb_left); | |
147 | } else if (swap_offset > ext->end) { | |
148 | /* Try to merge */ | |
149 | if (swap_offset == ext->end + 1) { | |
150 | ext->end++; | |
151 | return 0; | |
152 | } | |
153 | new = &((*new)->rb_right); | |
154 | } else { | |
155 | /* It already is in the tree */ | |
156 | return -EINVAL; | |
157 | } | |
158 | } | |
159 | /* Add the new node and rebalance the tree. */ | |
160 | ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL); | |
161 | if (!ext) | |
162 | return -ENOMEM; | |
163 | ||
164 | ext->start = swap_offset; | |
165 | ext->end = swap_offset; | |
166 | rb_link_node(&ext->node, parent, new); | |
167 | rb_insert_color(&ext->node, &swsusp_extents); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | /** | |
172 | * alloc_swapdev_block - allocate a swap page and register that it has | |
173 | * been allocated, so that it can be freed in case of an error. | |
174 | */ | |
175 | ||
176 | sector_t alloc_swapdev_block(int swap) | |
177 | { | |
178 | unsigned long offset; | |
179 | ||
910321ea | 180 | offset = swp_offset(get_swap_page_of_type(swap)); |
0414f2ec NC |
181 | if (offset) { |
182 | if (swsusp_extents_insert(offset)) | |
910321ea | 183 | swap_free(swp_entry(swap, offset)); |
0414f2ec NC |
184 | else |
185 | return swapdev_block(swap, offset); | |
186 | } | |
187 | return 0; | |
188 | } | |
189 | ||
190 | /** | |
191 | * free_all_swap_pages - free swap pages allocated for saving image data. | |
90133673 | 192 | * It also frees the extents used to register which swap entries had been |
0414f2ec NC |
193 | * allocated. |
194 | */ | |
195 | ||
196 | void free_all_swap_pages(int swap) | |
197 | { | |
198 | struct rb_node *node; | |
199 | ||
200 | while ((node = swsusp_extents.rb_node)) { | |
201 | struct swsusp_extent *ext; | |
202 | unsigned long offset; | |
203 | ||
204 | ext = container_of(node, struct swsusp_extent, node); | |
205 | rb_erase(node, &swsusp_extents); | |
206 | for (offset = ext->start; offset <= ext->end; offset++) | |
910321ea | 207 | swap_free(swp_entry(swap, offset)); |
0414f2ec NC |
208 | |
209 | kfree(ext); | |
210 | } | |
211 | } | |
212 | ||
213 | int swsusp_swap_in_use(void) | |
214 | { | |
215 | return (swsusp_extents.rb_node != NULL); | |
216 | } | |
217 | ||
61159a31 | 218 | /* |
3fc6b34f | 219 | * General things |
61159a31 RW |
220 | */ |
221 | ||
222 | static unsigned short root_swap = 0xffff; | |
343df3c7 CH |
223 | static struct block_device *hib_resume_bdev; |
224 | ||
225 | struct hib_bio_batch { | |
226 | atomic_t count; | |
227 | wait_queue_head_t wait; | |
228 | int error; | |
229 | }; | |
230 | ||
231 | static void hib_init_batch(struct hib_bio_batch *hb) | |
232 | { | |
233 | atomic_set(&hb->count, 0); | |
234 | init_waitqueue_head(&hb->wait); | |
235 | hb->error = 0; | |
236 | } | |
237 | ||
4246a0b6 | 238 | static void hib_end_io(struct bio *bio) |
343df3c7 CH |
239 | { |
240 | struct hib_bio_batch *hb = bio->bi_private; | |
343df3c7 CH |
241 | struct page *page = bio->bi_io_vec[0].bv_page; |
242 | ||
4246a0b6 | 243 | if (bio->bi_error) { |
343df3c7 CH |
244 | printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n", |
245 | imajor(bio->bi_bdev->bd_inode), | |
246 | iminor(bio->bi_bdev->bd_inode), | |
247 | (unsigned long long)bio->bi_iter.bi_sector); | |
343df3c7 CH |
248 | } |
249 | ||
250 | if (bio_data_dir(bio) == WRITE) | |
251 | put_page(page); | |
f6cf0545 JM |
252 | else if (clean_pages_on_read) |
253 | flush_icache_range((unsigned long)page_address(page), | |
254 | (unsigned long)page_address(page) + PAGE_SIZE); | |
343df3c7 | 255 | |
4246a0b6 CH |
256 | if (bio->bi_error && !hb->error) |
257 | hb->error = bio->bi_error; | |
343df3c7 CH |
258 | if (atomic_dec_and_test(&hb->count)) |
259 | wake_up(&hb->wait); | |
260 | ||
261 | bio_put(bio); | |
262 | } | |
263 | ||
264 | static int hib_submit_io(int rw, pgoff_t page_off, void *addr, | |
265 | struct hib_bio_batch *hb) | |
266 | { | |
267 | struct page *page = virt_to_page(addr); | |
268 | struct bio *bio; | |
269 | int error = 0; | |
270 | ||
71baba4b | 271 | bio = bio_alloc(__GFP_RECLAIM | __GFP_HIGH, 1); |
343df3c7 CH |
272 | bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9); |
273 | bio->bi_bdev = hib_resume_bdev; | |
274 | ||
275 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { | |
276 | printk(KERN_ERR "PM: Adding page to bio failed at %llu\n", | |
277 | (unsigned long long)bio->bi_iter.bi_sector); | |
278 | bio_put(bio); | |
279 | return -EFAULT; | |
280 | } | |
281 | ||
282 | if (hb) { | |
283 | bio->bi_end_io = hib_end_io; | |
284 | bio->bi_private = hb; | |
285 | atomic_inc(&hb->count); | |
286 | submit_bio(rw, bio); | |
287 | } else { | |
288 | error = submit_bio_wait(rw, bio); | |
289 | bio_put(bio); | |
290 | } | |
291 | ||
292 | return error; | |
293 | } | |
294 | ||
295 | static int hib_wait_io(struct hib_bio_batch *hb) | |
296 | { | |
297 | wait_event(hb->wait, atomic_read(&hb->count) == 0); | |
298 | return hb->error; | |
299 | } | |
3fc6b34f | 300 | |
3fc6b34f RW |
301 | /* |
302 | * Saving part | |
303 | */ | |
61159a31 | 304 | |
51fb352b | 305 | static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags) |
61159a31 RW |
306 | { |
307 | int error; | |
308 | ||
343df3c7 | 309 | hib_submit_io(READ_SYNC, swsusp_resume_block, swsusp_header, NULL); |
1b29c164 VG |
310 | if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) || |
311 | !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) { | |
312 | memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10); | |
3624eb04 | 313 | memcpy(swsusp_header->sig, HIBERNATE_SIG, 10); |
51fb352b | 314 | swsusp_header->image = handle->first_sector; |
a634cc10 | 315 | swsusp_header->flags = flags; |
081a9d04 BS |
316 | if (flags & SF_CRC32_MODE) |
317 | swsusp_header->crc32 = handle->crc32; | |
343df3c7 | 318 | error = hib_submit_io(WRITE_SYNC, swsusp_resume_block, |
1b29c164 | 319 | swsusp_header, NULL); |
61159a31 | 320 | } else { |
23976728 | 321 | printk(KERN_ERR "PM: Swap header not found!\n"); |
61159a31 RW |
322 | error = -ENODEV; |
323 | } | |
324 | return error; | |
325 | } | |
326 | ||
327 | /** | |
328 | * swsusp_swap_check - check if the resume device is a swap device | |
329 | * and get its index (if so) | |
6f612af5 JS |
330 | * |
331 | * This is called before saving image | |
61159a31 | 332 | */ |
6f612af5 | 333 | static int swsusp_swap_check(void) |
61159a31 | 334 | { |
3aef83e0 RW |
335 | int res; |
336 | ||
7bf23687 | 337 | res = swap_type_of(swsusp_resume_device, swsusp_resume_block, |
8a0d613f | 338 | &hib_resume_bdev); |
3aef83e0 RW |
339 | if (res < 0) |
340 | return res; | |
341 | ||
342 | root_swap = res; | |
e525fd89 | 343 | res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL); |
7bf23687 RW |
344 | if (res) |
345 | return res; | |
3aef83e0 | 346 | |
8a0d613f | 347 | res = set_blocksize(hib_resume_bdev, PAGE_SIZE); |
3aef83e0 | 348 | if (res < 0) |
8a0d613f | 349 | blkdev_put(hib_resume_bdev, FMODE_WRITE); |
61159a31 | 350 | |
61159a31 RW |
351 | return res; |
352 | } | |
353 | ||
354 | /** | |
355 | * write_page - Write one page to given swap location. | |
356 | * @buf: Address we're writing. | |
357 | * @offset: Offset of the swap page we're writing to. | |
343df3c7 | 358 | * @hb: bio completion batch |
61159a31 RW |
359 | */ |
360 | ||
343df3c7 | 361 | static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb) |
61159a31 | 362 | { |
3aef83e0 | 363 | void *src; |
081a9d04 | 364 | int ret; |
3aef83e0 RW |
365 | |
366 | if (!offset) | |
367 | return -ENOSPC; | |
368 | ||
343df3c7 | 369 | if (hb) { |
71baba4b | 370 | src = (void *)__get_free_page(__GFP_RECLAIM | __GFP_NOWARN | |
5a21d489 | 371 | __GFP_NORETRY); |
3aef83e0 | 372 | if (src) { |
3ecb01df | 373 | copy_page(src, buf); |
3aef83e0 | 374 | } else { |
343df3c7 | 375 | ret = hib_wait_io(hb); /* Free pages */ |
081a9d04 BS |
376 | if (ret) |
377 | return ret; | |
71baba4b | 378 | src = (void *)__get_free_page(__GFP_RECLAIM | |
5a21d489 BS |
379 | __GFP_NOWARN | |
380 | __GFP_NORETRY); | |
081a9d04 BS |
381 | if (src) { |
382 | copy_page(src, buf); | |
383 | } else { | |
384 | WARN_ON_ONCE(1); | |
343df3c7 | 385 | hb = NULL; /* Go synchronous */ |
081a9d04 BS |
386 | src = buf; |
387 | } | |
ab954160 | 388 | } |
3aef83e0 RW |
389 | } else { |
390 | src = buf; | |
61159a31 | 391 | } |
343df3c7 | 392 | return hib_submit_io(WRITE_SYNC, offset, src, hb); |
61159a31 RW |
393 | } |
394 | ||
61159a31 RW |
395 | static void release_swap_writer(struct swap_map_handle *handle) |
396 | { | |
397 | if (handle->cur) | |
398 | free_page((unsigned long)handle->cur); | |
399 | handle->cur = NULL; | |
61159a31 RW |
400 | } |
401 | ||
402 | static int get_swap_writer(struct swap_map_handle *handle) | |
403 | { | |
6f612af5 JS |
404 | int ret; |
405 | ||
406 | ret = swsusp_swap_check(); | |
407 | if (ret) { | |
408 | if (ret != -ENOSPC) | |
409 | printk(KERN_ERR "PM: Cannot find swap device, try " | |
410 | "swapon -a.\n"); | |
411 | return ret; | |
412 | } | |
61159a31 | 413 | handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL); |
6f612af5 JS |
414 | if (!handle->cur) { |
415 | ret = -ENOMEM; | |
416 | goto err_close; | |
417 | } | |
d1d241cc | 418 | handle->cur_swap = alloc_swapdev_block(root_swap); |
61159a31 | 419 | if (!handle->cur_swap) { |
6f612af5 JS |
420 | ret = -ENOSPC; |
421 | goto err_rel; | |
61159a31 RW |
422 | } |
423 | handle->k = 0; | |
f8262d47 | 424 | handle->reqd_free_pages = reqd_free_pages(); |
51fb352b | 425 | handle->first_sector = handle->cur_swap; |
61159a31 | 426 | return 0; |
6f612af5 JS |
427 | err_rel: |
428 | release_swap_writer(handle); | |
429 | err_close: | |
430 | swsusp_close(FMODE_WRITE); | |
431 | return ret; | |
61159a31 RW |
432 | } |
433 | ||
ab954160 | 434 | static int swap_write_page(struct swap_map_handle *handle, void *buf, |
343df3c7 | 435 | struct hib_bio_batch *hb) |
ab954160 AM |
436 | { |
437 | int error = 0; | |
3aef83e0 | 438 | sector_t offset; |
61159a31 RW |
439 | |
440 | if (!handle->cur) | |
441 | return -EINVAL; | |
d1d241cc | 442 | offset = alloc_swapdev_block(root_swap); |
343df3c7 | 443 | error = write_page(buf, offset, hb); |
61159a31 RW |
444 | if (error) |
445 | return error; | |
446 | handle->cur->entries[handle->k++] = offset; | |
447 | if (handle->k >= MAP_PAGE_ENTRIES) { | |
d1d241cc | 448 | offset = alloc_swapdev_block(root_swap); |
61159a31 RW |
449 | if (!offset) |
450 | return -ENOSPC; | |
451 | handle->cur->next_swap = offset; | |
343df3c7 | 452 | error = write_page(handle->cur, handle->cur_swap, hb); |
61159a31 | 453 | if (error) |
ab954160 | 454 | goto out; |
3ecb01df | 455 | clear_page(handle->cur); |
61159a31 RW |
456 | handle->cur_swap = offset; |
457 | handle->k = 0; | |
5a21d489 | 458 | |
343df3c7 CH |
459 | if (hb && low_free_pages() <= handle->reqd_free_pages) { |
460 | error = hib_wait_io(hb); | |
5a21d489 BS |
461 | if (error) |
462 | goto out; | |
463 | /* | |
464 | * Recalculate the number of required free pages, to | |
465 | * make sure we never take more than half. | |
466 | */ | |
467 | handle->reqd_free_pages = reqd_free_pages(); | |
468 | } | |
081a9d04 | 469 | } |
59a49335 | 470 | out: |
ab954160 | 471 | return error; |
61159a31 RW |
472 | } |
473 | ||
474 | static int flush_swap_writer(struct swap_map_handle *handle) | |
475 | { | |
476 | if (handle->cur && handle->cur_swap) | |
ab954160 | 477 | return write_page(handle->cur, handle->cur_swap, NULL); |
61159a31 RW |
478 | else |
479 | return -EINVAL; | |
480 | } | |
481 | ||
6f612af5 JS |
482 | static int swap_writer_finish(struct swap_map_handle *handle, |
483 | unsigned int flags, int error) | |
484 | { | |
485 | if (!error) { | |
486 | flush_swap_writer(handle); | |
487 | printk(KERN_INFO "PM: S"); | |
488 | error = mark_swapfiles(handle, flags); | |
489 | printk("|\n"); | |
490 | } | |
491 | ||
492 | if (error) | |
493 | free_all_swap_pages(root_swap); | |
494 | release_swap_writer(handle); | |
495 | swsusp_close(FMODE_WRITE); | |
496 | ||
497 | return error; | |
498 | } | |
499 | ||
f996fc96 BS |
500 | /* We need to remember how much compressed data we need to read. */ |
501 | #define LZO_HEADER sizeof(size_t) | |
502 | ||
503 | /* Number of pages/bytes we'll compress at one time. */ | |
504 | #define LZO_UNC_PAGES 32 | |
505 | #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE) | |
506 | ||
507 | /* Number of pages/bytes we need for compressed data (worst case). */ | |
508 | #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \ | |
509 | LZO_HEADER, PAGE_SIZE) | |
510 | #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE) | |
511 | ||
081a9d04 BS |
512 | /* Maximum number of threads for compression/decompression. */ |
513 | #define LZO_THREADS 3 | |
514 | ||
5a21d489 BS |
515 | /* Minimum/maximum number of pages for read buffering. */ |
516 | #define LZO_MIN_RD_PAGES 1024 | |
517 | #define LZO_MAX_RD_PAGES 8192 | |
081a9d04 BS |
518 | |
519 | ||
61159a31 RW |
520 | /** |
521 | * save_image - save the suspend image data | |
522 | */ | |
523 | ||
524 | static int save_image(struct swap_map_handle *handle, | |
525 | struct snapshot_handle *snapshot, | |
3a4f7577 | 526 | unsigned int nr_to_write) |
61159a31 RW |
527 | { |
528 | unsigned int m; | |
529 | int ret; | |
3a4f7577 | 530 | int nr_pages; |
ab954160 | 531 | int err2; |
343df3c7 | 532 | struct hib_bio_batch hb; |
db597605 TR |
533 | ktime_t start; |
534 | ktime_t stop; | |
61159a31 | 535 | |
343df3c7 CH |
536 | hib_init_batch(&hb); |
537 | ||
d8150d35 | 538 | printk(KERN_INFO "PM: Saving image data pages (%u pages)...\n", |
23976728 | 539 | nr_to_write); |
d8150d35 | 540 | m = nr_to_write / 10; |
61159a31 RW |
541 | if (!m) |
542 | m = 1; | |
543 | nr_pages = 0; | |
db597605 | 544 | start = ktime_get(); |
4ff277f9 | 545 | while (1) { |
d3c1b24c | 546 | ret = snapshot_read_next(snapshot); |
4ff277f9 JS |
547 | if (ret <= 0) |
548 | break; | |
343df3c7 | 549 | ret = swap_write_page(handle, data_of(*snapshot), &hb); |
4ff277f9 JS |
550 | if (ret) |
551 | break; | |
552 | if (!(nr_pages % m)) | |
d8150d35 BS |
553 | printk(KERN_INFO "PM: Image saving progress: %3d%%\n", |
554 | nr_pages / m * 10); | |
4ff277f9 JS |
555 | nr_pages++; |
556 | } | |
343df3c7 | 557 | err2 = hib_wait_io(&hb); |
db597605 | 558 | stop = ktime_get(); |
4ff277f9 JS |
559 | if (!ret) |
560 | ret = err2; | |
561 | if (!ret) | |
d8150d35 | 562 | printk(KERN_INFO "PM: Image saving done.\n"); |
db597605 | 563 | swsusp_show_speed(start, stop, nr_to_write, "Wrote"); |
4ff277f9 | 564 | return ret; |
61159a31 RW |
565 | } |
566 | ||
081a9d04 BS |
567 | /** |
568 | * Structure used for CRC32. | |
569 | */ | |
570 | struct crc_data { | |
571 | struct task_struct *thr; /* thread */ | |
572 | atomic_t ready; /* ready to start flag */ | |
573 | atomic_t stop; /* ready to stop flag */ | |
574 | unsigned run_threads; /* nr current threads */ | |
575 | wait_queue_head_t go; /* start crc update */ | |
576 | wait_queue_head_t done; /* crc update done */ | |
577 | u32 *crc32; /* points to handle's crc32 */ | |
578 | size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */ | |
579 | unsigned char *unc[LZO_THREADS]; /* uncompressed data */ | |
580 | }; | |
581 | ||
582 | /** | |
583 | * CRC32 update function that runs in its own thread. | |
584 | */ | |
585 | static int crc32_threadfn(void *data) | |
586 | { | |
587 | struct crc_data *d = data; | |
588 | unsigned i; | |
589 | ||
590 | while (1) { | |
591 | wait_event(d->go, atomic_read(&d->ready) || | |
592 | kthread_should_stop()); | |
593 | if (kthread_should_stop()) { | |
594 | d->thr = NULL; | |
595 | atomic_set(&d->stop, 1); | |
596 | wake_up(&d->done); | |
597 | break; | |
598 | } | |
599 | atomic_set(&d->ready, 0); | |
600 | ||
601 | for (i = 0; i < d->run_threads; i++) | |
602 | *d->crc32 = crc32_le(*d->crc32, | |
603 | d->unc[i], *d->unc_len[i]); | |
604 | atomic_set(&d->stop, 1); | |
605 | wake_up(&d->done); | |
606 | } | |
607 | return 0; | |
608 | } | |
609 | /** | |
610 | * Structure used for LZO data compression. | |
611 | */ | |
612 | struct cmp_data { | |
613 | struct task_struct *thr; /* thread */ | |
614 | atomic_t ready; /* ready to start flag */ | |
615 | atomic_t stop; /* ready to stop flag */ | |
616 | int ret; /* return code */ | |
617 | wait_queue_head_t go; /* start compression */ | |
618 | wait_queue_head_t done; /* compression done */ | |
619 | size_t unc_len; /* uncompressed length */ | |
620 | size_t cmp_len; /* compressed length */ | |
621 | unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */ | |
622 | unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */ | |
623 | unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */ | |
624 | }; | |
625 | ||
626 | /** | |
627 | * Compression function that runs in its own thread. | |
628 | */ | |
629 | static int lzo_compress_threadfn(void *data) | |
630 | { | |
631 | struct cmp_data *d = data; | |
632 | ||
633 | while (1) { | |
634 | wait_event(d->go, atomic_read(&d->ready) || | |
635 | kthread_should_stop()); | |
636 | if (kthread_should_stop()) { | |
637 | d->thr = NULL; | |
638 | d->ret = -1; | |
639 | atomic_set(&d->stop, 1); | |
640 | wake_up(&d->done); | |
641 | break; | |
642 | } | |
643 | atomic_set(&d->ready, 0); | |
644 | ||
645 | d->ret = lzo1x_1_compress(d->unc, d->unc_len, | |
646 | d->cmp + LZO_HEADER, &d->cmp_len, | |
647 | d->wrk); | |
648 | atomic_set(&d->stop, 1); | |
649 | wake_up(&d->done); | |
650 | } | |
651 | return 0; | |
652 | } | |
f996fc96 BS |
653 | |
654 | /** | |
655 | * save_image_lzo - Save the suspend image data compressed with LZO. | |
057b0a75 | 656 | * @handle: Swap map handle to use for saving the image. |
f996fc96 BS |
657 | * @snapshot: Image to read data from. |
658 | * @nr_to_write: Number of pages to save. | |
659 | */ | |
660 | static int save_image_lzo(struct swap_map_handle *handle, | |
661 | struct snapshot_handle *snapshot, | |
662 | unsigned int nr_to_write) | |
663 | { | |
664 | unsigned int m; | |
665 | int ret = 0; | |
666 | int nr_pages; | |
667 | int err2; | |
343df3c7 | 668 | struct hib_bio_batch hb; |
db597605 TR |
669 | ktime_t start; |
670 | ktime_t stop; | |
081a9d04 BS |
671 | size_t off; |
672 | unsigned thr, run_threads, nr_threads; | |
673 | unsigned char *page = NULL; | |
674 | struct cmp_data *data = NULL; | |
675 | struct crc_data *crc = NULL; | |
676 | ||
343df3c7 CH |
677 | hib_init_batch(&hb); |
678 | ||
081a9d04 BS |
679 | /* |
680 | * We'll limit the number of threads for compression to limit memory | |
681 | * footprint. | |
682 | */ | |
683 | nr_threads = num_online_cpus() - 1; | |
684 | nr_threads = clamp_val(nr_threads, 1, LZO_THREADS); | |
f996fc96 | 685 | |
71baba4b | 686 | page = (void *)__get_free_page(__GFP_RECLAIM | __GFP_HIGH); |
f996fc96 BS |
687 | if (!page) { |
688 | printk(KERN_ERR "PM: Failed to allocate LZO page\n"); | |
081a9d04 BS |
689 | ret = -ENOMEM; |
690 | goto out_clean; | |
f996fc96 BS |
691 | } |
692 | ||
081a9d04 BS |
693 | data = vmalloc(sizeof(*data) * nr_threads); |
694 | if (!data) { | |
695 | printk(KERN_ERR "PM: Failed to allocate LZO data\n"); | |
696 | ret = -ENOMEM; | |
697 | goto out_clean; | |
f996fc96 | 698 | } |
081a9d04 BS |
699 | for (thr = 0; thr < nr_threads; thr++) |
700 | memset(&data[thr], 0, offsetof(struct cmp_data, go)); | |
f996fc96 | 701 | |
081a9d04 BS |
702 | crc = kmalloc(sizeof(*crc), GFP_KERNEL); |
703 | if (!crc) { | |
704 | printk(KERN_ERR "PM: Failed to allocate crc\n"); | |
705 | ret = -ENOMEM; | |
706 | goto out_clean; | |
707 | } | |
708 | memset(crc, 0, offsetof(struct crc_data, go)); | |
709 | ||
710 | /* | |
711 | * Start the compression threads. | |
712 | */ | |
713 | for (thr = 0; thr < nr_threads; thr++) { | |
714 | init_waitqueue_head(&data[thr].go); | |
715 | init_waitqueue_head(&data[thr].done); | |
716 | ||
717 | data[thr].thr = kthread_run(lzo_compress_threadfn, | |
718 | &data[thr], | |
719 | "image_compress/%u", thr); | |
720 | if (IS_ERR(data[thr].thr)) { | |
721 | data[thr].thr = NULL; | |
722 | printk(KERN_ERR | |
723 | "PM: Cannot start compression threads\n"); | |
724 | ret = -ENOMEM; | |
725 | goto out_clean; | |
726 | } | |
f996fc96 BS |
727 | } |
728 | ||
081a9d04 BS |
729 | /* |
730 | * Start the CRC32 thread. | |
731 | */ | |
732 | init_waitqueue_head(&crc->go); | |
733 | init_waitqueue_head(&crc->done); | |
734 | ||
735 | handle->crc32 = 0; | |
736 | crc->crc32 = &handle->crc32; | |
737 | for (thr = 0; thr < nr_threads; thr++) { | |
738 | crc->unc[thr] = data[thr].unc; | |
739 | crc->unc_len[thr] = &data[thr].unc_len; | |
740 | } | |
741 | ||
742 | crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32"); | |
743 | if (IS_ERR(crc->thr)) { | |
744 | crc->thr = NULL; | |
745 | printk(KERN_ERR "PM: Cannot start CRC32 thread\n"); | |
746 | ret = -ENOMEM; | |
747 | goto out_clean; | |
f996fc96 BS |
748 | } |
749 | ||
5a21d489 BS |
750 | /* |
751 | * Adjust the number of required free pages after all allocations have | |
752 | * been done. We don't want to run out of pages when writing. | |
753 | */ | |
754 | handle->reqd_free_pages = reqd_free_pages(); | |
755 | ||
f996fc96 | 756 | printk(KERN_INFO |
081a9d04 | 757 | "PM: Using %u thread(s) for compression.\n" |
d8150d35 | 758 | "PM: Compressing and saving image data (%u pages)...\n", |
081a9d04 | 759 | nr_threads, nr_to_write); |
d8150d35 | 760 | m = nr_to_write / 10; |
f996fc96 BS |
761 | if (!m) |
762 | m = 1; | |
763 | nr_pages = 0; | |
db597605 | 764 | start = ktime_get(); |
f996fc96 | 765 | for (;;) { |
081a9d04 BS |
766 | for (thr = 0; thr < nr_threads; thr++) { |
767 | for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) { | |
768 | ret = snapshot_read_next(snapshot); | |
769 | if (ret < 0) | |
770 | goto out_finish; | |
771 | ||
772 | if (!ret) | |
773 | break; | |
774 | ||
775 | memcpy(data[thr].unc + off, | |
776 | data_of(*snapshot), PAGE_SIZE); | |
777 | ||
778 | if (!(nr_pages % m)) | |
d8150d35 BS |
779 | printk(KERN_INFO |
780 | "PM: Image saving progress: " | |
781 | "%3d%%\n", | |
782 | nr_pages / m * 10); | |
081a9d04 BS |
783 | nr_pages++; |
784 | } | |
785 | if (!off) | |
f996fc96 BS |
786 | break; |
787 | ||
081a9d04 | 788 | data[thr].unc_len = off; |
f996fc96 | 789 | |
081a9d04 BS |
790 | atomic_set(&data[thr].ready, 1); |
791 | wake_up(&data[thr].go); | |
f996fc96 BS |
792 | } |
793 | ||
081a9d04 | 794 | if (!thr) |
f996fc96 BS |
795 | break; |
796 | ||
081a9d04 BS |
797 | crc->run_threads = thr; |
798 | atomic_set(&crc->ready, 1); | |
799 | wake_up(&crc->go); | |
f996fc96 | 800 | |
081a9d04 BS |
801 | for (run_threads = thr, thr = 0; thr < run_threads; thr++) { |
802 | wait_event(data[thr].done, | |
803 | atomic_read(&data[thr].stop)); | |
804 | atomic_set(&data[thr].stop, 0); | |
f996fc96 | 805 | |
081a9d04 | 806 | ret = data[thr].ret; |
f996fc96 | 807 | |
081a9d04 BS |
808 | if (ret < 0) { |
809 | printk(KERN_ERR "PM: LZO compression failed\n"); | |
810 | goto out_finish; | |
811 | } | |
f996fc96 | 812 | |
081a9d04 BS |
813 | if (unlikely(!data[thr].cmp_len || |
814 | data[thr].cmp_len > | |
815 | lzo1x_worst_compress(data[thr].unc_len))) { | |
816 | printk(KERN_ERR | |
817 | "PM: Invalid LZO compressed length\n"); | |
818 | ret = -1; | |
f996fc96 | 819 | goto out_finish; |
081a9d04 BS |
820 | } |
821 | ||
822 | *(size_t *)data[thr].cmp = data[thr].cmp_len; | |
823 | ||
824 | /* | |
825 | * Given we are writing one page at a time to disk, we | |
826 | * copy that much from the buffer, although the last | |
827 | * bit will likely be smaller than full page. This is | |
828 | * OK - we saved the length of the compressed data, so | |
829 | * any garbage at the end will be discarded when we | |
830 | * read it. | |
831 | */ | |
832 | for (off = 0; | |
833 | off < LZO_HEADER + data[thr].cmp_len; | |
834 | off += PAGE_SIZE) { | |
835 | memcpy(page, data[thr].cmp + off, PAGE_SIZE); | |
836 | ||
343df3c7 | 837 | ret = swap_write_page(handle, page, &hb); |
081a9d04 BS |
838 | if (ret) |
839 | goto out_finish; | |
840 | } | |
f996fc96 | 841 | } |
081a9d04 BS |
842 | |
843 | wait_event(crc->done, atomic_read(&crc->stop)); | |
844 | atomic_set(&crc->stop, 0); | |
f996fc96 BS |
845 | } |
846 | ||
847 | out_finish: | |
343df3c7 | 848 | err2 = hib_wait_io(&hb); |
db597605 | 849 | stop = ktime_get(); |
f996fc96 BS |
850 | if (!ret) |
851 | ret = err2; | |
d8150d35 BS |
852 | if (!ret) |
853 | printk(KERN_INFO "PM: Image saving done.\n"); | |
db597605 | 854 | swsusp_show_speed(start, stop, nr_to_write, "Wrote"); |
081a9d04 BS |
855 | out_clean: |
856 | if (crc) { | |
857 | if (crc->thr) | |
858 | kthread_stop(crc->thr); | |
859 | kfree(crc); | |
860 | } | |
861 | if (data) { | |
862 | for (thr = 0; thr < nr_threads; thr++) | |
863 | if (data[thr].thr) | |
864 | kthread_stop(data[thr].thr); | |
865 | vfree(data); | |
866 | } | |
867 | if (page) free_page((unsigned long)page); | |
f996fc96 BS |
868 | |
869 | return ret; | |
870 | } | |
871 | ||
61159a31 RW |
872 | /** |
873 | * enough_swap - Make sure we have enough swap to save the image. | |
874 | * | |
875 | * Returns TRUE or FALSE after checking the total amount of swap | |
876 | * space avaiable from the resume partition. | |
877 | */ | |
878 | ||
f996fc96 | 879 | static int enough_swap(unsigned int nr_pages, unsigned int flags) |
61159a31 RW |
880 | { |
881 | unsigned int free_swap = count_swap_pages(root_swap, 1); | |
f996fc96 | 882 | unsigned int required; |
61159a31 | 883 | |
23976728 | 884 | pr_debug("PM: Free swap pages: %u\n", free_swap); |
f996fc96 | 885 | |
ee34a370 | 886 | required = PAGES_FOR_IO + nr_pages; |
f996fc96 | 887 | return free_swap > required; |
61159a31 RW |
888 | } |
889 | ||
890 | /** | |
891 | * swsusp_write - Write entire image and metadata. | |
a634cc10 | 892 | * @flags: flags to pass to the "boot" kernel in the image header |
61159a31 RW |
893 | * |
894 | * It is important _NOT_ to umount filesystems at this point. We want | |
895 | * them synced (in case something goes wrong) but we DO not want to mark | |
896 | * filesystem clean: it is not. (And it does not matter, if we resume | |
897 | * correctly, we'll mark system clean, anyway.) | |
898 | */ | |
899 | ||
a634cc10 | 900 | int swsusp_write(unsigned int flags) |
61159a31 RW |
901 | { |
902 | struct swap_map_handle handle; | |
903 | struct snapshot_handle snapshot; | |
904 | struct swsusp_info *header; | |
6f612af5 | 905 | unsigned long pages; |
61159a31 RW |
906 | int error; |
907 | ||
6f612af5 JS |
908 | pages = snapshot_get_image_size(); |
909 | error = get_swap_writer(&handle); | |
3aef83e0 | 910 | if (error) { |
6f612af5 | 911 | printk(KERN_ERR "PM: Cannot get swap writer\n"); |
61159a31 RW |
912 | return error; |
913 | } | |
ee34a370 BS |
914 | if (flags & SF_NOCOMPRESS_MODE) { |
915 | if (!enough_swap(pages, flags)) { | |
916 | printk(KERN_ERR "PM: Not enough free swap\n"); | |
917 | error = -ENOSPC; | |
918 | goto out_finish; | |
919 | } | |
6f612af5 | 920 | } |
61159a31 | 921 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
d3c1b24c | 922 | error = snapshot_read_next(&snapshot); |
3aef83e0 RW |
923 | if (error < PAGE_SIZE) { |
924 | if (error >= 0) | |
925 | error = -EFAULT; | |
926 | ||
6f612af5 | 927 | goto out_finish; |
3aef83e0 | 928 | } |
61159a31 | 929 | header = (struct swsusp_info *)data_of(snapshot); |
6f612af5 | 930 | error = swap_write_page(&handle, header, NULL); |
f996fc96 BS |
931 | if (!error) { |
932 | error = (flags & SF_NOCOMPRESS_MODE) ? | |
933 | save_image(&handle, &snapshot, pages - 1) : | |
934 | save_image_lzo(&handle, &snapshot, pages - 1); | |
935 | } | |
6f612af5 JS |
936 | out_finish: |
937 | error = swap_writer_finish(&handle, flags, error); | |
61159a31 RW |
938 | return error; |
939 | } | |
940 | ||
61159a31 RW |
941 | /** |
942 | * The following functions allow us to read data using a swap map | |
943 | * in a file-alike way | |
944 | */ | |
945 | ||
946 | static void release_swap_reader(struct swap_map_handle *handle) | |
947 | { | |
081a9d04 BS |
948 | struct swap_map_page_list *tmp; |
949 | ||
950 | while (handle->maps) { | |
951 | if (handle->maps->map) | |
952 | free_page((unsigned long)handle->maps->map); | |
953 | tmp = handle->maps; | |
954 | handle->maps = handle->maps->next; | |
955 | kfree(tmp); | |
956 | } | |
61159a31 RW |
957 | handle->cur = NULL; |
958 | } | |
959 | ||
6f612af5 JS |
960 | static int get_swap_reader(struct swap_map_handle *handle, |
961 | unsigned int *flags_p) | |
61159a31 RW |
962 | { |
963 | int error; | |
081a9d04 BS |
964 | struct swap_map_page_list *tmp, *last; |
965 | sector_t offset; | |
61159a31 | 966 | |
6f612af5 JS |
967 | *flags_p = swsusp_header->flags; |
968 | ||
969 | if (!swsusp_header->image) /* how can this happen? */ | |
61159a31 | 970 | return -EINVAL; |
3aef83e0 | 971 | |
081a9d04 BS |
972 | handle->cur = NULL; |
973 | last = handle->maps = NULL; | |
974 | offset = swsusp_header->image; | |
975 | while (offset) { | |
976 | tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL); | |
977 | if (!tmp) { | |
978 | release_swap_reader(handle); | |
979 | return -ENOMEM; | |
980 | } | |
981 | memset(tmp, 0, sizeof(*tmp)); | |
982 | if (!handle->maps) | |
983 | handle->maps = tmp; | |
984 | if (last) | |
985 | last->next = tmp; | |
986 | last = tmp; | |
987 | ||
988 | tmp->map = (struct swap_map_page *) | |
71baba4b | 989 | __get_free_page(__GFP_RECLAIM | __GFP_HIGH); |
081a9d04 BS |
990 | if (!tmp->map) { |
991 | release_swap_reader(handle); | |
992 | return -ENOMEM; | |
993 | } | |
3aef83e0 | 994 | |
343df3c7 | 995 | error = hib_submit_io(READ_SYNC, offset, tmp->map, NULL); |
081a9d04 BS |
996 | if (error) { |
997 | release_swap_reader(handle); | |
998 | return error; | |
999 | } | |
1000 | offset = tmp->map->next_swap; | |
61159a31 RW |
1001 | } |
1002 | handle->k = 0; | |
081a9d04 | 1003 | handle->cur = handle->maps->map; |
61159a31 RW |
1004 | return 0; |
1005 | } | |
1006 | ||
546e0d27 | 1007 | static int swap_read_page(struct swap_map_handle *handle, void *buf, |
343df3c7 | 1008 | struct hib_bio_batch *hb) |
61159a31 | 1009 | { |
3aef83e0 | 1010 | sector_t offset; |
61159a31 | 1011 | int error; |
081a9d04 | 1012 | struct swap_map_page_list *tmp; |
61159a31 RW |
1013 | |
1014 | if (!handle->cur) | |
1015 | return -EINVAL; | |
1016 | offset = handle->cur->entries[handle->k]; | |
1017 | if (!offset) | |
1018 | return -EFAULT; | |
343df3c7 | 1019 | error = hib_submit_io(READ_SYNC, offset, buf, hb); |
61159a31 RW |
1020 | if (error) |
1021 | return error; | |
1022 | if (++handle->k >= MAP_PAGE_ENTRIES) { | |
1023 | handle->k = 0; | |
081a9d04 BS |
1024 | free_page((unsigned long)handle->maps->map); |
1025 | tmp = handle->maps; | |
1026 | handle->maps = handle->maps->next; | |
1027 | kfree(tmp); | |
1028 | if (!handle->maps) | |
61159a31 | 1029 | release_swap_reader(handle); |
081a9d04 BS |
1030 | else |
1031 | handle->cur = handle->maps->map; | |
61159a31 RW |
1032 | } |
1033 | return error; | |
1034 | } | |
1035 | ||
6f612af5 JS |
1036 | static int swap_reader_finish(struct swap_map_handle *handle) |
1037 | { | |
1038 | release_swap_reader(handle); | |
1039 | ||
1040 | return 0; | |
1041 | } | |
1042 | ||
61159a31 RW |
1043 | /** |
1044 | * load_image - load the image using the swap map handle | |
1045 | * @handle and the snapshot handle @snapshot | |
1046 | * (assume there are @nr_pages pages to load) | |
1047 | */ | |
1048 | ||
1049 | static int load_image(struct swap_map_handle *handle, | |
1050 | struct snapshot_handle *snapshot, | |
546e0d27 | 1051 | unsigned int nr_to_read) |
61159a31 RW |
1052 | { |
1053 | unsigned int m; | |
081a9d04 | 1054 | int ret = 0; |
db597605 TR |
1055 | ktime_t start; |
1056 | ktime_t stop; | |
343df3c7 | 1057 | struct hib_bio_batch hb; |
546e0d27 AM |
1058 | int err2; |
1059 | unsigned nr_pages; | |
61159a31 | 1060 | |
343df3c7 CH |
1061 | hib_init_batch(&hb); |
1062 | ||
f6cf0545 | 1063 | clean_pages_on_read = true; |
d8150d35 | 1064 | printk(KERN_INFO "PM: Loading image data pages (%u pages)...\n", |
23976728 | 1065 | nr_to_read); |
d8150d35 | 1066 | m = nr_to_read / 10; |
61159a31 RW |
1067 | if (!m) |
1068 | m = 1; | |
1069 | nr_pages = 0; | |
db597605 | 1070 | start = ktime_get(); |
546e0d27 | 1071 | for ( ; ; ) { |
081a9d04 BS |
1072 | ret = snapshot_write_next(snapshot); |
1073 | if (ret <= 0) | |
546e0d27 | 1074 | break; |
343df3c7 | 1075 | ret = swap_read_page(handle, data_of(*snapshot), &hb); |
081a9d04 | 1076 | if (ret) |
546e0d27 AM |
1077 | break; |
1078 | if (snapshot->sync_read) | |
343df3c7 | 1079 | ret = hib_wait_io(&hb); |
081a9d04 | 1080 | if (ret) |
546e0d27 AM |
1081 | break; |
1082 | if (!(nr_pages % m)) | |
d8150d35 BS |
1083 | printk(KERN_INFO "PM: Image loading progress: %3d%%\n", |
1084 | nr_pages / m * 10); | |
546e0d27 AM |
1085 | nr_pages++; |
1086 | } | |
343df3c7 | 1087 | err2 = hib_wait_io(&hb); |
db597605 | 1088 | stop = ktime_get(); |
081a9d04 BS |
1089 | if (!ret) |
1090 | ret = err2; | |
1091 | if (!ret) { | |
d8150d35 | 1092 | printk(KERN_INFO "PM: Image loading done.\n"); |
8357376d | 1093 | snapshot_write_finalize(snapshot); |
e655a250 | 1094 | if (!snapshot_image_loaded(snapshot)) |
081a9d04 | 1095 | ret = -ENODATA; |
d8150d35 | 1096 | } |
db597605 | 1097 | swsusp_show_speed(start, stop, nr_to_read, "Read"); |
081a9d04 BS |
1098 | return ret; |
1099 | } | |
1100 | ||
1101 | /** | |
1102 | * Structure used for LZO data decompression. | |
1103 | */ | |
1104 | struct dec_data { | |
1105 | struct task_struct *thr; /* thread */ | |
1106 | atomic_t ready; /* ready to start flag */ | |
1107 | atomic_t stop; /* ready to stop flag */ | |
1108 | int ret; /* return code */ | |
1109 | wait_queue_head_t go; /* start decompression */ | |
1110 | wait_queue_head_t done; /* decompression done */ | |
1111 | size_t unc_len; /* uncompressed length */ | |
1112 | size_t cmp_len; /* compressed length */ | |
1113 | unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */ | |
1114 | unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */ | |
1115 | }; | |
1116 | ||
1117 | /** | |
1118 | * Deompression function that runs in its own thread. | |
1119 | */ | |
1120 | static int lzo_decompress_threadfn(void *data) | |
1121 | { | |
1122 | struct dec_data *d = data; | |
1123 | ||
1124 | while (1) { | |
1125 | wait_event(d->go, atomic_read(&d->ready) || | |
1126 | kthread_should_stop()); | |
1127 | if (kthread_should_stop()) { | |
1128 | d->thr = NULL; | |
1129 | d->ret = -1; | |
1130 | atomic_set(&d->stop, 1); | |
1131 | wake_up(&d->done); | |
1132 | break; | |
1133 | } | |
1134 | atomic_set(&d->ready, 0); | |
1135 | ||
1136 | d->unc_len = LZO_UNC_SIZE; | |
1137 | d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len, | |
1138 | d->unc, &d->unc_len); | |
f6cf0545 JM |
1139 | if (clean_pages_on_decompress) |
1140 | flush_icache_range((unsigned long)d->unc, | |
1141 | (unsigned long)d->unc + d->unc_len); | |
1142 | ||
081a9d04 BS |
1143 | atomic_set(&d->stop, 1); |
1144 | wake_up(&d->done); | |
1145 | } | |
1146 | return 0; | |
61159a31 RW |
1147 | } |
1148 | ||
f996fc96 BS |
1149 | /** |
1150 | * load_image_lzo - Load compressed image data and decompress them with LZO. | |
1151 | * @handle: Swap map handle to use for loading data. | |
1152 | * @snapshot: Image to copy uncompressed data into. | |
1153 | * @nr_to_read: Number of pages to load. | |
1154 | */ | |
1155 | static int load_image_lzo(struct swap_map_handle *handle, | |
1156 | struct snapshot_handle *snapshot, | |
1157 | unsigned int nr_to_read) | |
1158 | { | |
1159 | unsigned int m; | |
081a9d04 BS |
1160 | int ret = 0; |
1161 | int eof = 0; | |
343df3c7 | 1162 | struct hib_bio_batch hb; |
db597605 TR |
1163 | ktime_t start; |
1164 | ktime_t stop; | |
f996fc96 | 1165 | unsigned nr_pages; |
081a9d04 BS |
1166 | size_t off; |
1167 | unsigned i, thr, run_threads, nr_threads; | |
1168 | unsigned ring = 0, pg = 0, ring_size = 0, | |
1169 | have = 0, want, need, asked = 0; | |
5a21d489 | 1170 | unsigned long read_pages = 0; |
081a9d04 BS |
1171 | unsigned char **page = NULL; |
1172 | struct dec_data *data = NULL; | |
1173 | struct crc_data *crc = NULL; | |
1174 | ||
343df3c7 CH |
1175 | hib_init_batch(&hb); |
1176 | ||
081a9d04 BS |
1177 | /* |
1178 | * We'll limit the number of threads for decompression to limit memory | |
1179 | * footprint. | |
1180 | */ | |
1181 | nr_threads = num_online_cpus() - 1; | |
1182 | nr_threads = clamp_val(nr_threads, 1, LZO_THREADS); | |
1183 | ||
5a21d489 | 1184 | page = vmalloc(sizeof(*page) * LZO_MAX_RD_PAGES); |
081a9d04 BS |
1185 | if (!page) { |
1186 | printk(KERN_ERR "PM: Failed to allocate LZO page\n"); | |
1187 | ret = -ENOMEM; | |
1188 | goto out_clean; | |
1189 | } | |
9f339caf | 1190 | |
081a9d04 BS |
1191 | data = vmalloc(sizeof(*data) * nr_threads); |
1192 | if (!data) { | |
1193 | printk(KERN_ERR "PM: Failed to allocate LZO data\n"); | |
1194 | ret = -ENOMEM; | |
1195 | goto out_clean; | |
1196 | } | |
1197 | for (thr = 0; thr < nr_threads; thr++) | |
1198 | memset(&data[thr], 0, offsetof(struct dec_data, go)); | |
9f339caf | 1199 | |
081a9d04 BS |
1200 | crc = kmalloc(sizeof(*crc), GFP_KERNEL); |
1201 | if (!crc) { | |
1202 | printk(KERN_ERR "PM: Failed to allocate crc\n"); | |
1203 | ret = -ENOMEM; | |
1204 | goto out_clean; | |
1205 | } | |
1206 | memset(crc, 0, offsetof(struct crc_data, go)); | |
1207 | ||
f6cf0545 JM |
1208 | clean_pages_on_decompress = true; |
1209 | ||
081a9d04 BS |
1210 | /* |
1211 | * Start the decompression threads. | |
1212 | */ | |
1213 | for (thr = 0; thr < nr_threads; thr++) { | |
1214 | init_waitqueue_head(&data[thr].go); | |
1215 | init_waitqueue_head(&data[thr].done); | |
1216 | ||
1217 | data[thr].thr = kthread_run(lzo_decompress_threadfn, | |
1218 | &data[thr], | |
1219 | "image_decompress/%u", thr); | |
1220 | if (IS_ERR(data[thr].thr)) { | |
1221 | data[thr].thr = NULL; | |
1222 | printk(KERN_ERR | |
1223 | "PM: Cannot start decompression threads\n"); | |
1224 | ret = -ENOMEM; | |
1225 | goto out_clean; | |
9f339caf | 1226 | } |
f996fc96 BS |
1227 | } |
1228 | ||
081a9d04 BS |
1229 | /* |
1230 | * Start the CRC32 thread. | |
1231 | */ | |
1232 | init_waitqueue_head(&crc->go); | |
1233 | init_waitqueue_head(&crc->done); | |
1234 | ||
1235 | handle->crc32 = 0; | |
1236 | crc->crc32 = &handle->crc32; | |
1237 | for (thr = 0; thr < nr_threads; thr++) { | |
1238 | crc->unc[thr] = data[thr].unc; | |
1239 | crc->unc_len[thr] = &data[thr].unc_len; | |
f996fc96 BS |
1240 | } |
1241 | ||
081a9d04 BS |
1242 | crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32"); |
1243 | if (IS_ERR(crc->thr)) { | |
1244 | crc->thr = NULL; | |
1245 | printk(KERN_ERR "PM: Cannot start CRC32 thread\n"); | |
1246 | ret = -ENOMEM; | |
1247 | goto out_clean; | |
1248 | } | |
9f339caf | 1249 | |
081a9d04 | 1250 | /* |
5a21d489 BS |
1251 | * Set the number of pages for read buffering. |
1252 | * This is complete guesswork, because we'll only know the real | |
1253 | * picture once prepare_image() is called, which is much later on | |
1254 | * during the image load phase. We'll assume the worst case and | |
1255 | * say that none of the image pages are from high memory. | |
081a9d04 | 1256 | */ |
5a21d489 BS |
1257 | if (low_free_pages() > snapshot_get_image_size()) |
1258 | read_pages = (low_free_pages() - snapshot_get_image_size()) / 2; | |
1259 | read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES); | |
9f339caf | 1260 | |
081a9d04 BS |
1261 | for (i = 0; i < read_pages; i++) { |
1262 | page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ? | |
71baba4b MG |
1263 | __GFP_RECLAIM | __GFP_HIGH : |
1264 | __GFP_RECLAIM | __GFP_NOWARN | | |
1265 | __GFP_NORETRY); | |
5a21d489 | 1266 | |
081a9d04 BS |
1267 | if (!page[i]) { |
1268 | if (i < LZO_CMP_PAGES) { | |
1269 | ring_size = i; | |
1270 | printk(KERN_ERR | |
1271 | "PM: Failed to allocate LZO pages\n"); | |
1272 | ret = -ENOMEM; | |
1273 | goto out_clean; | |
1274 | } else { | |
1275 | break; | |
1276 | } | |
1277 | } | |
f996fc96 | 1278 | } |
081a9d04 | 1279 | want = ring_size = i; |
f996fc96 BS |
1280 | |
1281 | printk(KERN_INFO | |
081a9d04 | 1282 | "PM: Using %u thread(s) for decompression.\n" |
d8150d35 | 1283 | "PM: Loading and decompressing image data (%u pages)...\n", |
081a9d04 | 1284 | nr_threads, nr_to_read); |
d8150d35 | 1285 | m = nr_to_read / 10; |
f996fc96 BS |
1286 | if (!m) |
1287 | m = 1; | |
1288 | nr_pages = 0; | |
db597605 | 1289 | start = ktime_get(); |
f996fc96 | 1290 | |
081a9d04 BS |
1291 | ret = snapshot_write_next(snapshot); |
1292 | if (ret <= 0) | |
f996fc96 BS |
1293 | goto out_finish; |
1294 | ||
081a9d04 BS |
1295 | for(;;) { |
1296 | for (i = 0; !eof && i < want; i++) { | |
343df3c7 | 1297 | ret = swap_read_page(handle, page[ring], &hb); |
081a9d04 BS |
1298 | if (ret) { |
1299 | /* | |
1300 | * On real read error, finish. On end of data, | |
1301 | * set EOF flag and just exit the read loop. | |
1302 | */ | |
1303 | if (handle->cur && | |
1304 | handle->cur->entries[handle->k]) { | |
1305 | goto out_finish; | |
1306 | } else { | |
1307 | eof = 1; | |
1308 | break; | |
1309 | } | |
1310 | } | |
1311 | if (++ring >= ring_size) | |
1312 | ring = 0; | |
f996fc96 | 1313 | } |
081a9d04 BS |
1314 | asked += i; |
1315 | want -= i; | |
f996fc96 | 1316 | |
081a9d04 BS |
1317 | /* |
1318 | * We are out of data, wait for some more. | |
1319 | */ | |
1320 | if (!have) { | |
1321 | if (!asked) | |
1322 | break; | |
1323 | ||
343df3c7 | 1324 | ret = hib_wait_io(&hb); |
081a9d04 | 1325 | if (ret) |
f996fc96 | 1326 | goto out_finish; |
081a9d04 BS |
1327 | have += asked; |
1328 | asked = 0; | |
1329 | if (eof) | |
1330 | eof = 2; | |
9f339caf | 1331 | } |
f996fc96 | 1332 | |
081a9d04 BS |
1333 | if (crc->run_threads) { |
1334 | wait_event(crc->done, atomic_read(&crc->stop)); | |
1335 | atomic_set(&crc->stop, 0); | |
1336 | crc->run_threads = 0; | |
f996fc96 BS |
1337 | } |
1338 | ||
081a9d04 BS |
1339 | for (thr = 0; have && thr < nr_threads; thr++) { |
1340 | data[thr].cmp_len = *(size_t *)page[pg]; | |
1341 | if (unlikely(!data[thr].cmp_len || | |
1342 | data[thr].cmp_len > | |
1343 | lzo1x_worst_compress(LZO_UNC_SIZE))) { | |
1344 | printk(KERN_ERR | |
1345 | "PM: Invalid LZO compressed length\n"); | |
1346 | ret = -1; | |
1347 | goto out_finish; | |
1348 | } | |
1349 | ||
1350 | need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER, | |
1351 | PAGE_SIZE); | |
1352 | if (need > have) { | |
1353 | if (eof > 1) { | |
1354 | ret = -1; | |
1355 | goto out_finish; | |
1356 | } | |
1357 | break; | |
1358 | } | |
1359 | ||
1360 | for (off = 0; | |
1361 | off < LZO_HEADER + data[thr].cmp_len; | |
1362 | off += PAGE_SIZE) { | |
1363 | memcpy(data[thr].cmp + off, | |
1364 | page[pg], PAGE_SIZE); | |
1365 | have--; | |
1366 | want++; | |
1367 | if (++pg >= ring_size) | |
1368 | pg = 0; | |
1369 | } | |
1370 | ||
1371 | atomic_set(&data[thr].ready, 1); | |
1372 | wake_up(&data[thr].go); | |
f996fc96 BS |
1373 | } |
1374 | ||
081a9d04 BS |
1375 | /* |
1376 | * Wait for more data while we are decompressing. | |
1377 | */ | |
1378 | if (have < LZO_CMP_PAGES && asked) { | |
343df3c7 | 1379 | ret = hib_wait_io(&hb); |
081a9d04 BS |
1380 | if (ret) |
1381 | goto out_finish; | |
1382 | have += asked; | |
1383 | asked = 0; | |
1384 | if (eof) | |
1385 | eof = 2; | |
f996fc96 BS |
1386 | } |
1387 | ||
081a9d04 BS |
1388 | for (run_threads = thr, thr = 0; thr < run_threads; thr++) { |
1389 | wait_event(data[thr].done, | |
1390 | atomic_read(&data[thr].stop)); | |
1391 | atomic_set(&data[thr].stop, 0); | |
1392 | ||
1393 | ret = data[thr].ret; | |
f996fc96 | 1394 | |
081a9d04 BS |
1395 | if (ret < 0) { |
1396 | printk(KERN_ERR | |
1397 | "PM: LZO decompression failed\n"); | |
1398 | goto out_finish; | |
1399 | } | |
f996fc96 | 1400 | |
081a9d04 BS |
1401 | if (unlikely(!data[thr].unc_len || |
1402 | data[thr].unc_len > LZO_UNC_SIZE || | |
1403 | data[thr].unc_len & (PAGE_SIZE - 1))) { | |
1404 | printk(KERN_ERR | |
1405 | "PM: Invalid LZO uncompressed length\n"); | |
1406 | ret = -1; | |
f996fc96 | 1407 | goto out_finish; |
081a9d04 BS |
1408 | } |
1409 | ||
1410 | for (off = 0; | |
1411 | off < data[thr].unc_len; off += PAGE_SIZE) { | |
1412 | memcpy(data_of(*snapshot), | |
1413 | data[thr].unc + off, PAGE_SIZE); | |
1414 | ||
1415 | if (!(nr_pages % m)) | |
d8150d35 BS |
1416 | printk(KERN_INFO |
1417 | "PM: Image loading progress: " | |
1418 | "%3d%%\n", | |
1419 | nr_pages / m * 10); | |
081a9d04 BS |
1420 | nr_pages++; |
1421 | ||
1422 | ret = snapshot_write_next(snapshot); | |
1423 | if (ret <= 0) { | |
1424 | crc->run_threads = thr + 1; | |
1425 | atomic_set(&crc->ready, 1); | |
1426 | wake_up(&crc->go); | |
1427 | goto out_finish; | |
1428 | } | |
1429 | } | |
f996fc96 | 1430 | } |
081a9d04 BS |
1431 | |
1432 | crc->run_threads = thr; | |
1433 | atomic_set(&crc->ready, 1); | |
1434 | wake_up(&crc->go); | |
f996fc96 BS |
1435 | } |
1436 | ||
1437 | out_finish: | |
081a9d04 BS |
1438 | if (crc->run_threads) { |
1439 | wait_event(crc->done, atomic_read(&crc->stop)); | |
1440 | atomic_set(&crc->stop, 0); | |
1441 | } | |
db597605 | 1442 | stop = ktime_get(); |
081a9d04 | 1443 | if (!ret) { |
d8150d35 | 1444 | printk(KERN_INFO "PM: Image loading done.\n"); |
f996fc96 BS |
1445 | snapshot_write_finalize(snapshot); |
1446 | if (!snapshot_image_loaded(snapshot)) | |
081a9d04 BS |
1447 | ret = -ENODATA; |
1448 | if (!ret) { | |
1449 | if (swsusp_header->flags & SF_CRC32_MODE) { | |
1450 | if(handle->crc32 != swsusp_header->crc32) { | |
1451 | printk(KERN_ERR | |
1452 | "PM: Invalid image CRC32!\n"); | |
1453 | ret = -ENODATA; | |
1454 | } | |
1455 | } | |
1456 | } | |
d8150d35 | 1457 | } |
db597605 | 1458 | swsusp_show_speed(start, stop, nr_to_read, "Read"); |
081a9d04 BS |
1459 | out_clean: |
1460 | for (i = 0; i < ring_size; i++) | |
9f339caf | 1461 | free_page((unsigned long)page[i]); |
081a9d04 BS |
1462 | if (crc) { |
1463 | if (crc->thr) | |
1464 | kthread_stop(crc->thr); | |
1465 | kfree(crc); | |
1466 | } | |
1467 | if (data) { | |
1468 | for (thr = 0; thr < nr_threads; thr++) | |
1469 | if (data[thr].thr) | |
1470 | kthread_stop(data[thr].thr); | |
1471 | vfree(data); | |
1472 | } | |
6c45de0d | 1473 | vfree(page); |
f996fc96 | 1474 | |
081a9d04 | 1475 | return ret; |
f996fc96 BS |
1476 | } |
1477 | ||
a634cc10 RW |
1478 | /** |
1479 | * swsusp_read - read the hibernation image. | |
1480 | * @flags_p: flags passed by the "frozen" kernel in the image header should | |
b595076a | 1481 | * be written into this memory location |
a634cc10 RW |
1482 | */ |
1483 | ||
1484 | int swsusp_read(unsigned int *flags_p) | |
61159a31 RW |
1485 | { |
1486 | int error; | |
1487 | struct swap_map_handle handle; | |
1488 | struct snapshot_handle snapshot; | |
1489 | struct swsusp_info *header; | |
1490 | ||
61159a31 | 1491 | memset(&snapshot, 0, sizeof(struct snapshot_handle)); |
d3c1b24c | 1492 | error = snapshot_write_next(&snapshot); |
61159a31 RW |
1493 | if (error < PAGE_SIZE) |
1494 | return error < 0 ? error : -EFAULT; | |
1495 | header = (struct swsusp_info *)data_of(snapshot); | |
6f612af5 JS |
1496 | error = get_swap_reader(&handle, flags_p); |
1497 | if (error) | |
1498 | goto end; | |
61159a31 | 1499 | if (!error) |
546e0d27 | 1500 | error = swap_read_page(&handle, header, NULL); |
f996fc96 BS |
1501 | if (!error) { |
1502 | error = (*flags_p & SF_NOCOMPRESS_MODE) ? | |
1503 | load_image(&handle, &snapshot, header->pages - 1) : | |
1504 | load_image_lzo(&handle, &snapshot, header->pages - 1); | |
1505 | } | |
6f612af5 JS |
1506 | swap_reader_finish(&handle); |
1507 | end: | |
61159a31 | 1508 | if (!error) |
23976728 | 1509 | pr_debug("PM: Image successfully loaded\n"); |
61159a31 | 1510 | else |
23976728 | 1511 | pr_debug("PM: Error %d resuming\n", error); |
61159a31 RW |
1512 | return error; |
1513 | } | |
1514 | ||
1515 | /** | |
1516 | * swsusp_check - Check for swsusp signature in the resume device | |
1517 | */ | |
1518 | ||
1519 | int swsusp_check(void) | |
1520 | { | |
1521 | int error; | |
1522 | ||
d4d77629 TH |
1523 | hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device, |
1524 | FMODE_READ, NULL); | |
8a0d613f JS |
1525 | if (!IS_ERR(hib_resume_bdev)) { |
1526 | set_blocksize(hib_resume_bdev, PAGE_SIZE); | |
3ecb01df | 1527 | clear_page(swsusp_header); |
343df3c7 | 1528 | error = hib_submit_io(READ_SYNC, swsusp_resume_block, |
1b29c164 | 1529 | swsusp_header, NULL); |
9a154d9d | 1530 | if (error) |
76b57e61 | 1531 | goto put; |
9a154d9d | 1532 | |
3624eb04 | 1533 | if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) { |
1b29c164 | 1534 | memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10); |
61159a31 | 1535 | /* Reset swap signature now */ |
343df3c7 | 1536 | error = hib_submit_io(WRITE_SYNC, swsusp_resume_block, |
1b29c164 | 1537 | swsusp_header, NULL); |
61159a31 | 1538 | } else { |
76b57e61 | 1539 | error = -EINVAL; |
61159a31 | 1540 | } |
76b57e61 JS |
1541 | |
1542 | put: | |
61159a31 | 1543 | if (error) |
8a0d613f | 1544 | blkdev_put(hib_resume_bdev, FMODE_READ); |
61159a31 | 1545 | else |
d0941ead | 1546 | pr_debug("PM: Image signature found, resuming\n"); |
61159a31 | 1547 | } else { |
8a0d613f | 1548 | error = PTR_ERR(hib_resume_bdev); |
61159a31 RW |
1549 | } |
1550 | ||
1551 | if (error) | |
d0941ead | 1552 | pr_debug("PM: Image not found (code %d)\n", error); |
61159a31 RW |
1553 | |
1554 | return error; | |
1555 | } | |
1556 | ||
1557 | /** | |
1558 | * swsusp_close - close swap device. | |
1559 | */ | |
1560 | ||
c2dd0dae | 1561 | void swsusp_close(fmode_t mode) |
61159a31 | 1562 | { |
8a0d613f | 1563 | if (IS_ERR(hib_resume_bdev)) { |
23976728 | 1564 | pr_debug("PM: Image device not initialised\n"); |
61159a31 RW |
1565 | return; |
1566 | } | |
1567 | ||
8a0d613f | 1568 | blkdev_put(hib_resume_bdev, mode); |
61159a31 | 1569 | } |
1b29c164 | 1570 | |
62c552cc BS |
1571 | /** |
1572 | * swsusp_unmark - Unmark swsusp signature in the resume device | |
1573 | */ | |
1574 | ||
1575 | #ifdef CONFIG_SUSPEND | |
1576 | int swsusp_unmark(void) | |
1577 | { | |
1578 | int error; | |
1579 | ||
343df3c7 | 1580 | hib_submit_io(READ_SYNC, swsusp_resume_block, swsusp_header, NULL); |
62c552cc BS |
1581 | if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) { |
1582 | memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10); | |
343df3c7 | 1583 | error = hib_submit_io(WRITE_SYNC, swsusp_resume_block, |
62c552cc BS |
1584 | swsusp_header, NULL); |
1585 | } else { | |
1586 | printk(KERN_ERR "PM: Cannot find swsusp signature!\n"); | |
1587 | error = -ENODEV; | |
1588 | } | |
1589 | ||
1590 | /* | |
1591 | * We just returned from suspend, we don't need the image any more. | |
1592 | */ | |
1593 | free_all_swap_pages(root_swap); | |
1594 | ||
1595 | return error; | |
1596 | } | |
1597 | #endif | |
1598 | ||
1b29c164 VG |
1599 | static int swsusp_header_init(void) |
1600 | { | |
1601 | swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL); | |
1602 | if (!swsusp_header) | |
1603 | panic("Could not allocate memory for swsusp_header\n"); | |
1604 | return 0; | |
1605 | } | |
1606 | ||
1607 | core_initcall(swsusp_header_init); |