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