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