1 /* 2 * Compressed RAM block device 3 * 4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta 5 * 2012, 2013 Minchan Kim 6 * 7 * This code is released using a dual license strategy: BSD/GPL 8 * You can choose the licence that better fits your requirements. 9 * 10 * Released under the terms of 3-clause BSD License 11 * Released under the terms of GNU General Public License Version 2.0 12 * 13 */ 14 15 #define KMSG_COMPONENT "zram" 16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 17 18 #include <linux/module.h> 19 #include <linux/kernel.h> 20 #include <linux/bio.h> 21 #include <linux/bitops.h> 22 #include <linux/blkdev.h> 23 #include <linux/buffer_head.h> 24 #include <linux/device.h> 25 #include <linux/highmem.h> 26 #include <linux/slab.h> 27 #include <linux/backing-dev.h> 28 #include <linux/string.h> 29 #include <linux/vmalloc.h> 30 #include <linux/err.h> 31 #include <linux/idr.h> 32 #include <linux/sysfs.h> 33 #include <linux/debugfs.h> 34 #include <linux/cpuhotplug.h> 35 #include <linux/part_stat.h> 36 37 #include "zram_drv.h" 38 39 static DEFINE_IDR(zram_index_idr); 40 /* idr index must be protected */ 41 static DEFINE_MUTEX(zram_index_mutex); 42 43 static int zram_major; 44 static const char *default_compressor = CONFIG_ZRAM_DEF_COMP; 45 46 /* Module params (documentation at end) */ 47 static unsigned int num_devices = 1; 48 /* 49 * Pages that compress to sizes equals or greater than this are stored 50 * uncompressed in memory. 51 */ 52 static size_t huge_class_size; 53 54 static const struct block_device_operations zram_devops; 55 static const struct block_device_operations zram_wb_devops; 56 57 static void zram_free_page(struct zram *zram, size_t index); 58 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, 59 u32 index, int offset, struct bio *bio); 60 61 62 static int zram_slot_trylock(struct zram *zram, u32 index) 63 { 64 return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags); 65 } 66 67 static void zram_slot_lock(struct zram *zram, u32 index) 68 { 69 bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags); 70 } 71 72 static void zram_slot_unlock(struct zram *zram, u32 index) 73 { 74 bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags); 75 } 76 77 static inline bool init_done(struct zram *zram) 78 { 79 return zram->disksize; 80 } 81 82 static inline struct zram *dev_to_zram(struct device *dev) 83 { 84 return (struct zram *)dev_to_disk(dev)->private_data; 85 } 86 87 static unsigned long zram_get_handle(struct zram *zram, u32 index) 88 { 89 return zram->table[index].handle; 90 } 91 92 static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle) 93 { 94 zram->table[index].handle = handle; 95 } 96 97 /* flag operations require table entry bit_spin_lock() being held */ 98 static bool zram_test_flag(struct zram *zram, u32 index, 99 enum zram_pageflags flag) 100 { 101 return zram->table[index].flags & BIT(flag); 102 } 103 104 static void zram_set_flag(struct zram *zram, u32 index, 105 enum zram_pageflags flag) 106 { 107 zram->table[index].flags |= BIT(flag); 108 } 109 110 static void zram_clear_flag(struct zram *zram, u32 index, 111 enum zram_pageflags flag) 112 { 113 zram->table[index].flags &= ~BIT(flag); 114 } 115 116 static inline void zram_set_element(struct zram *zram, u32 index, 117 unsigned long element) 118 { 119 zram->table[index].element = element; 120 } 121 122 static unsigned long zram_get_element(struct zram *zram, u32 index) 123 { 124 return zram->table[index].element; 125 } 126 127 static size_t zram_get_obj_size(struct zram *zram, u32 index) 128 { 129 return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1); 130 } 131 132 static void zram_set_obj_size(struct zram *zram, 133 u32 index, size_t size) 134 { 135 unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT; 136 137 zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size; 138 } 139 140 static inline bool zram_allocated(struct zram *zram, u32 index) 141 { 142 return zram_get_obj_size(zram, index) || 143 zram_test_flag(zram, index, ZRAM_SAME) || 144 zram_test_flag(zram, index, ZRAM_WB); 145 } 146 147 #if PAGE_SIZE != 4096 148 static inline bool is_partial_io(struct bio_vec *bvec) 149 { 150 return bvec->bv_len != PAGE_SIZE; 151 } 152 #else 153 static inline bool is_partial_io(struct bio_vec *bvec) 154 { 155 return false; 156 } 157 #endif 158 159 /* 160 * Check if request is within bounds and aligned on zram logical blocks. 161 */ 162 static inline bool valid_io_request(struct zram *zram, 163 sector_t start, unsigned int size) 164 { 165 u64 end, bound; 166 167 /* unaligned request */ 168 if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1))) 169 return false; 170 if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1))) 171 return false; 172 173 end = start + (size >> SECTOR_SHIFT); 174 bound = zram->disksize >> SECTOR_SHIFT; 175 /* out of range range */ 176 if (unlikely(start >= bound || end > bound || start > end)) 177 return false; 178 179 /* I/O request is valid */ 180 return true; 181 } 182 183 static void update_position(u32 *index, int *offset, struct bio_vec *bvec) 184 { 185 *index += (*offset + bvec->bv_len) / PAGE_SIZE; 186 *offset = (*offset + bvec->bv_len) % PAGE_SIZE; 187 } 188 189 static inline void update_used_max(struct zram *zram, 190 const unsigned long pages) 191 { 192 unsigned long old_max, cur_max; 193 194 old_max = atomic_long_read(&zram->stats.max_used_pages); 195 196 do { 197 cur_max = old_max; 198 if (pages > cur_max) 199 old_max = atomic_long_cmpxchg( 200 &zram->stats.max_used_pages, cur_max, pages); 201 } while (old_max != cur_max); 202 } 203 204 static inline void zram_fill_page(void *ptr, unsigned long len, 205 unsigned long value) 206 { 207 WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long))); 208 memset_l(ptr, value, len / sizeof(unsigned long)); 209 } 210 211 static bool page_same_filled(void *ptr, unsigned long *element) 212 { 213 unsigned long *page; 214 unsigned long val; 215 unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1; 216 217 page = (unsigned long *)ptr; 218 val = page[0]; 219 220 if (val != page[last_pos]) 221 return false; 222 223 for (pos = 1; pos < last_pos; pos++) { 224 if (val != page[pos]) 225 return false; 226 } 227 228 *element = val; 229 230 return true; 231 } 232 233 static ssize_t initstate_show(struct device *dev, 234 struct device_attribute *attr, char *buf) 235 { 236 u32 val; 237 struct zram *zram = dev_to_zram(dev); 238 239 down_read(&zram->init_lock); 240 val = init_done(zram); 241 up_read(&zram->init_lock); 242 243 return scnprintf(buf, PAGE_SIZE, "%u\n", val); 244 } 245 246 static ssize_t disksize_show(struct device *dev, 247 struct device_attribute *attr, char *buf) 248 { 249 struct zram *zram = dev_to_zram(dev); 250 251 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize); 252 } 253 254 static ssize_t mem_limit_store(struct device *dev, 255 struct device_attribute *attr, const char *buf, size_t len) 256 { 257 u64 limit; 258 char *tmp; 259 struct zram *zram = dev_to_zram(dev); 260 261 limit = memparse(buf, &tmp); 262 if (buf == tmp) /* no chars parsed, invalid input */ 263 return -EINVAL; 264 265 down_write(&zram->init_lock); 266 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT; 267 up_write(&zram->init_lock); 268 269 return len; 270 } 271 272 static ssize_t mem_used_max_store(struct device *dev, 273 struct device_attribute *attr, const char *buf, size_t len) 274 { 275 int err; 276 unsigned long val; 277 struct zram *zram = dev_to_zram(dev); 278 279 err = kstrtoul(buf, 10, &val); 280 if (err || val != 0) 281 return -EINVAL; 282 283 down_read(&zram->init_lock); 284 if (init_done(zram)) { 285 atomic_long_set(&zram->stats.max_used_pages, 286 zs_get_total_pages(zram->mem_pool)); 287 } 288 up_read(&zram->init_lock); 289 290 return len; 291 } 292 293 /* 294 * Mark all pages which are older than or equal to cutoff as IDLE. 295 * Callers should hold the zram init lock in read mode 296 */ 297 static void mark_idle(struct zram *zram, ktime_t cutoff) 298 { 299 int is_idle = 1; 300 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT; 301 int index; 302 303 for (index = 0; index < nr_pages; index++) { 304 /* 305 * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race. 306 * See the comment in writeback_store. 307 */ 308 zram_slot_lock(zram, index); 309 if (zram_allocated(zram, index) && 310 !zram_test_flag(zram, index, ZRAM_UNDER_WB)) { 311 #ifdef CONFIG_ZRAM_MEMORY_TRACKING 312 is_idle = !cutoff || ktime_after(cutoff, zram->table[index].ac_time); 313 #endif 314 if (is_idle) 315 zram_set_flag(zram, index, ZRAM_IDLE); 316 } 317 zram_slot_unlock(zram, index); 318 } 319 } 320 321 static ssize_t idle_store(struct device *dev, 322 struct device_attribute *attr, const char *buf, size_t len) 323 { 324 struct zram *zram = dev_to_zram(dev); 325 ktime_t cutoff_time = 0; 326 ssize_t rv = -EINVAL; 327 328 if (!sysfs_streq(buf, "all")) { 329 /* 330 * If it did not parse as 'all' try to treat it as an integer when 331 * we have memory tracking enabled. 332 */ 333 u64 age_sec; 334 335 if (IS_ENABLED(CONFIG_ZRAM_MEMORY_TRACKING) && !kstrtoull(buf, 0, &age_sec)) 336 cutoff_time = ktime_sub(ktime_get_boottime(), 337 ns_to_ktime(age_sec * NSEC_PER_SEC)); 338 else 339 goto out; 340 } 341 342 down_read(&zram->init_lock); 343 if (!init_done(zram)) 344 goto out_unlock; 345 346 /* A cutoff_time of 0 marks everything as idle, this is the "all" behavior */ 347 mark_idle(zram, cutoff_time); 348 rv = len; 349 350 out_unlock: 351 up_read(&zram->init_lock); 352 out: 353 return rv; 354 } 355 356 #ifdef CONFIG_ZRAM_WRITEBACK 357 static ssize_t writeback_limit_enable_store(struct device *dev, 358 struct device_attribute *attr, const char *buf, size_t len) 359 { 360 struct zram *zram = dev_to_zram(dev); 361 u64 val; 362 ssize_t ret = -EINVAL; 363 364 if (kstrtoull(buf, 10, &val)) 365 return ret; 366 367 down_read(&zram->init_lock); 368 spin_lock(&zram->wb_limit_lock); 369 zram->wb_limit_enable = val; 370 spin_unlock(&zram->wb_limit_lock); 371 up_read(&zram->init_lock); 372 ret = len; 373 374 return ret; 375 } 376 377 static ssize_t writeback_limit_enable_show(struct device *dev, 378 struct device_attribute *attr, char *buf) 379 { 380 bool val; 381 struct zram *zram = dev_to_zram(dev); 382 383 down_read(&zram->init_lock); 384 spin_lock(&zram->wb_limit_lock); 385 val = zram->wb_limit_enable; 386 spin_unlock(&zram->wb_limit_lock); 387 up_read(&zram->init_lock); 388 389 return scnprintf(buf, PAGE_SIZE, "%d\n", val); 390 } 391 392 static ssize_t writeback_limit_store(struct device *dev, 393 struct device_attribute *attr, const char *buf, size_t len) 394 { 395 struct zram *zram = dev_to_zram(dev); 396 u64 val; 397 ssize_t ret = -EINVAL; 398 399 if (kstrtoull(buf, 10, &val)) 400 return ret; 401 402 down_read(&zram->init_lock); 403 spin_lock(&zram->wb_limit_lock); 404 zram->bd_wb_limit = val; 405 spin_unlock(&zram->wb_limit_lock); 406 up_read(&zram->init_lock); 407 ret = len; 408 409 return ret; 410 } 411 412 static ssize_t writeback_limit_show(struct device *dev, 413 struct device_attribute *attr, char *buf) 414 { 415 u64 val; 416 struct zram *zram = dev_to_zram(dev); 417 418 down_read(&zram->init_lock); 419 spin_lock(&zram->wb_limit_lock); 420 val = zram->bd_wb_limit; 421 spin_unlock(&zram->wb_limit_lock); 422 up_read(&zram->init_lock); 423 424 return scnprintf(buf, PAGE_SIZE, "%llu\n", val); 425 } 426 427 static void reset_bdev(struct zram *zram) 428 { 429 struct block_device *bdev; 430 431 if (!zram->backing_dev) 432 return; 433 434 bdev = zram->bdev; 435 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 436 /* hope filp_close flush all of IO */ 437 filp_close(zram->backing_dev, NULL); 438 zram->backing_dev = NULL; 439 zram->bdev = NULL; 440 zram->disk->fops = &zram_devops; 441 kvfree(zram->bitmap); 442 zram->bitmap = NULL; 443 } 444 445 static ssize_t backing_dev_show(struct device *dev, 446 struct device_attribute *attr, char *buf) 447 { 448 struct file *file; 449 struct zram *zram = dev_to_zram(dev); 450 char *p; 451 ssize_t ret; 452 453 down_read(&zram->init_lock); 454 file = zram->backing_dev; 455 if (!file) { 456 memcpy(buf, "none\n", 5); 457 up_read(&zram->init_lock); 458 return 5; 459 } 460 461 p = file_path(file, buf, PAGE_SIZE - 1); 462 if (IS_ERR(p)) { 463 ret = PTR_ERR(p); 464 goto out; 465 } 466 467 ret = strlen(p); 468 memmove(buf, p, ret); 469 buf[ret++] = '\n'; 470 out: 471 up_read(&zram->init_lock); 472 return ret; 473 } 474 475 static ssize_t backing_dev_store(struct device *dev, 476 struct device_attribute *attr, const char *buf, size_t len) 477 { 478 char *file_name; 479 size_t sz; 480 struct file *backing_dev = NULL; 481 struct inode *inode; 482 struct address_space *mapping; 483 unsigned int bitmap_sz; 484 unsigned long nr_pages, *bitmap = NULL; 485 struct block_device *bdev = NULL; 486 int err; 487 struct zram *zram = dev_to_zram(dev); 488 489 file_name = kmalloc(PATH_MAX, GFP_KERNEL); 490 if (!file_name) 491 return -ENOMEM; 492 493 down_write(&zram->init_lock); 494 if (init_done(zram)) { 495 pr_info("Can't setup backing device for initialized device\n"); 496 err = -EBUSY; 497 goto out; 498 } 499 500 strlcpy(file_name, buf, PATH_MAX); 501 /* ignore trailing newline */ 502 sz = strlen(file_name); 503 if (sz > 0 && file_name[sz - 1] == '\n') 504 file_name[sz - 1] = 0x00; 505 506 backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0); 507 if (IS_ERR(backing_dev)) { 508 err = PTR_ERR(backing_dev); 509 backing_dev = NULL; 510 goto out; 511 } 512 513 mapping = backing_dev->f_mapping; 514 inode = mapping->host; 515 516 /* Support only block device in this moment */ 517 if (!S_ISBLK(inode->i_mode)) { 518 err = -ENOTBLK; 519 goto out; 520 } 521 522 bdev = blkdev_get_by_dev(inode->i_rdev, 523 FMODE_READ | FMODE_WRITE | FMODE_EXCL, zram); 524 if (IS_ERR(bdev)) { 525 err = PTR_ERR(bdev); 526 bdev = NULL; 527 goto out; 528 } 529 530 nr_pages = i_size_read(inode) >> PAGE_SHIFT; 531 bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long); 532 bitmap = kvzalloc(bitmap_sz, GFP_KERNEL); 533 if (!bitmap) { 534 err = -ENOMEM; 535 goto out; 536 } 537 538 reset_bdev(zram); 539 540 zram->bdev = bdev; 541 zram->backing_dev = backing_dev; 542 zram->bitmap = bitmap; 543 zram->nr_pages = nr_pages; 544 /* 545 * With writeback feature, zram does asynchronous IO so it's no longer 546 * synchronous device so let's remove synchronous io flag. Othewise, 547 * upper layer(e.g., swap) could wait IO completion rather than 548 * (submit and return), which will cause system sluggish. 549 * Furthermore, when the IO function returns(e.g., swap_readpage), 550 * upper layer expects IO was done so it could deallocate the page 551 * freely but in fact, IO is going on so finally could cause 552 * use-after-free when the IO is really done. 553 */ 554 zram->disk->fops = &zram_wb_devops; 555 up_write(&zram->init_lock); 556 557 pr_info("setup backing device %s\n", file_name); 558 kfree(file_name); 559 560 return len; 561 out: 562 kvfree(bitmap); 563 564 if (bdev) 565 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 566 567 if (backing_dev) 568 filp_close(backing_dev, NULL); 569 570 up_write(&zram->init_lock); 571 572 kfree(file_name); 573 574 return err; 575 } 576 577 static unsigned long alloc_block_bdev(struct zram *zram) 578 { 579 unsigned long blk_idx = 1; 580 retry: 581 /* skip 0 bit to confuse zram.handle = 0 */ 582 blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx); 583 if (blk_idx == zram->nr_pages) 584 return 0; 585 586 if (test_and_set_bit(blk_idx, zram->bitmap)) 587 goto retry; 588 589 atomic64_inc(&zram->stats.bd_count); 590 return blk_idx; 591 } 592 593 static void free_block_bdev(struct zram *zram, unsigned long blk_idx) 594 { 595 int was_set; 596 597 was_set = test_and_clear_bit(blk_idx, zram->bitmap); 598 WARN_ON_ONCE(!was_set); 599 atomic64_dec(&zram->stats.bd_count); 600 } 601 602 static void zram_page_end_io(struct bio *bio) 603 { 604 struct page *page = bio_first_page_all(bio); 605 606 page_endio(page, op_is_write(bio_op(bio)), 607 blk_status_to_errno(bio->bi_status)); 608 bio_put(bio); 609 } 610 611 /* 612 * Returns 1 if the submission is successful. 613 */ 614 static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec, 615 unsigned long entry, struct bio *parent) 616 { 617 struct bio *bio; 618 619 bio = bio_alloc(zram->bdev, 1, parent ? parent->bi_opf : REQ_OP_READ, 620 GFP_NOIO); 621 if (!bio) 622 return -ENOMEM; 623 624 bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9); 625 if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) { 626 bio_put(bio); 627 return -EIO; 628 } 629 630 if (!parent) 631 bio->bi_end_io = zram_page_end_io; 632 else 633 bio_chain(bio, parent); 634 635 submit_bio(bio); 636 return 1; 637 } 638 639 #define PAGE_WB_SIG "page_index=" 640 641 #define PAGE_WRITEBACK 0 642 #define HUGE_WRITEBACK (1<<0) 643 #define IDLE_WRITEBACK (1<<1) 644 645 646 static ssize_t writeback_store(struct device *dev, 647 struct device_attribute *attr, const char *buf, size_t len) 648 { 649 struct zram *zram = dev_to_zram(dev); 650 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT; 651 unsigned long index = 0; 652 struct bio bio; 653 struct bio_vec bio_vec; 654 struct page *page; 655 ssize_t ret = len; 656 int mode, err; 657 unsigned long blk_idx = 0; 658 659 if (sysfs_streq(buf, "idle")) 660 mode = IDLE_WRITEBACK; 661 else if (sysfs_streq(buf, "huge")) 662 mode = HUGE_WRITEBACK; 663 else if (sysfs_streq(buf, "huge_idle")) 664 mode = IDLE_WRITEBACK | HUGE_WRITEBACK; 665 else { 666 if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1)) 667 return -EINVAL; 668 669 if (kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index) || 670 index >= nr_pages) 671 return -EINVAL; 672 673 nr_pages = 1; 674 mode = PAGE_WRITEBACK; 675 } 676 677 down_read(&zram->init_lock); 678 if (!init_done(zram)) { 679 ret = -EINVAL; 680 goto release_init_lock; 681 } 682 683 if (!zram->backing_dev) { 684 ret = -ENODEV; 685 goto release_init_lock; 686 } 687 688 page = alloc_page(GFP_KERNEL); 689 if (!page) { 690 ret = -ENOMEM; 691 goto release_init_lock; 692 } 693 694 for (; nr_pages != 0; index++, nr_pages--) { 695 struct bio_vec bvec; 696 697 bvec.bv_page = page; 698 bvec.bv_len = PAGE_SIZE; 699 bvec.bv_offset = 0; 700 701 spin_lock(&zram->wb_limit_lock); 702 if (zram->wb_limit_enable && !zram->bd_wb_limit) { 703 spin_unlock(&zram->wb_limit_lock); 704 ret = -EIO; 705 break; 706 } 707 spin_unlock(&zram->wb_limit_lock); 708 709 if (!blk_idx) { 710 blk_idx = alloc_block_bdev(zram); 711 if (!blk_idx) { 712 ret = -ENOSPC; 713 break; 714 } 715 } 716 717 zram_slot_lock(zram, index); 718 if (!zram_allocated(zram, index)) 719 goto next; 720 721 if (zram_test_flag(zram, index, ZRAM_WB) || 722 zram_test_flag(zram, index, ZRAM_SAME) || 723 zram_test_flag(zram, index, ZRAM_UNDER_WB)) 724 goto next; 725 726 if (mode & IDLE_WRITEBACK && 727 !zram_test_flag(zram, index, ZRAM_IDLE)) 728 goto next; 729 if (mode & HUGE_WRITEBACK && 730 !zram_test_flag(zram, index, ZRAM_HUGE)) 731 goto next; 732 /* 733 * Clearing ZRAM_UNDER_WB is duty of caller. 734 * IOW, zram_free_page never clear it. 735 */ 736 zram_set_flag(zram, index, ZRAM_UNDER_WB); 737 /* Need for hugepage writeback racing */ 738 zram_set_flag(zram, index, ZRAM_IDLE); 739 zram_slot_unlock(zram, index); 740 if (zram_bvec_read(zram, &bvec, index, 0, NULL)) { 741 zram_slot_lock(zram, index); 742 zram_clear_flag(zram, index, ZRAM_UNDER_WB); 743 zram_clear_flag(zram, index, ZRAM_IDLE); 744 zram_slot_unlock(zram, index); 745 continue; 746 } 747 748 bio_init(&bio, zram->bdev, &bio_vec, 1, 749 REQ_OP_WRITE | REQ_SYNC); 750 bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9); 751 752 bio_add_page(&bio, bvec.bv_page, bvec.bv_len, 753 bvec.bv_offset); 754 /* 755 * XXX: A single page IO would be inefficient for write 756 * but it would be not bad as starter. 757 */ 758 err = submit_bio_wait(&bio); 759 if (err) { 760 zram_slot_lock(zram, index); 761 zram_clear_flag(zram, index, ZRAM_UNDER_WB); 762 zram_clear_flag(zram, index, ZRAM_IDLE); 763 zram_slot_unlock(zram, index); 764 /* 765 * Return last IO error unless every IO were 766 * not suceeded. 767 */ 768 ret = err; 769 continue; 770 } 771 772 atomic64_inc(&zram->stats.bd_writes); 773 /* 774 * We released zram_slot_lock so need to check if the slot was 775 * changed. If there is freeing for the slot, we can catch it 776 * easily by zram_allocated. 777 * A subtle case is the slot is freed/reallocated/marked as 778 * ZRAM_IDLE again. To close the race, idle_store doesn't 779 * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB. 780 * Thus, we could close the race by checking ZRAM_IDLE bit. 781 */ 782 zram_slot_lock(zram, index); 783 if (!zram_allocated(zram, index) || 784 !zram_test_flag(zram, index, ZRAM_IDLE)) { 785 zram_clear_flag(zram, index, ZRAM_UNDER_WB); 786 zram_clear_flag(zram, index, ZRAM_IDLE); 787 goto next; 788 } 789 790 zram_free_page(zram, index); 791 zram_clear_flag(zram, index, ZRAM_UNDER_WB); 792 zram_set_flag(zram, index, ZRAM_WB); 793 zram_set_element(zram, index, blk_idx); 794 blk_idx = 0; 795 atomic64_inc(&zram->stats.pages_stored); 796 spin_lock(&zram->wb_limit_lock); 797 if (zram->wb_limit_enable && zram->bd_wb_limit > 0) 798 zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12); 799 spin_unlock(&zram->wb_limit_lock); 800 next: 801 zram_slot_unlock(zram, index); 802 } 803 804 if (blk_idx) 805 free_block_bdev(zram, blk_idx); 806 __free_page(page); 807 release_init_lock: 808 up_read(&zram->init_lock); 809 810 return ret; 811 } 812 813 struct zram_work { 814 struct work_struct work; 815 struct zram *zram; 816 unsigned long entry; 817 struct bio *bio; 818 struct bio_vec bvec; 819 }; 820 821 #if PAGE_SIZE != 4096 822 static void zram_sync_read(struct work_struct *work) 823 { 824 struct zram_work *zw = container_of(work, struct zram_work, work); 825 struct zram *zram = zw->zram; 826 unsigned long entry = zw->entry; 827 struct bio *bio = zw->bio; 828 829 read_from_bdev_async(zram, &zw->bvec, entry, bio); 830 } 831 832 /* 833 * Block layer want one ->submit_bio to be active at a time, so if we use 834 * chained IO with parent IO in same context, it's a deadlock. To avoid that, 835 * use a worker thread context. 836 */ 837 static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec, 838 unsigned long entry, struct bio *bio) 839 { 840 struct zram_work work; 841 842 work.bvec = *bvec; 843 work.zram = zram; 844 work.entry = entry; 845 work.bio = bio; 846 847 INIT_WORK_ONSTACK(&work.work, zram_sync_read); 848 queue_work(system_unbound_wq, &work.work); 849 flush_work(&work.work); 850 destroy_work_on_stack(&work.work); 851 852 return 1; 853 } 854 #else 855 static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec, 856 unsigned long entry, struct bio *bio) 857 { 858 WARN_ON(1); 859 return -EIO; 860 } 861 #endif 862 863 static int read_from_bdev(struct zram *zram, struct bio_vec *bvec, 864 unsigned long entry, struct bio *parent, bool sync) 865 { 866 atomic64_inc(&zram->stats.bd_reads); 867 if (sync) 868 return read_from_bdev_sync(zram, bvec, entry, parent); 869 else 870 return read_from_bdev_async(zram, bvec, entry, parent); 871 } 872 #else 873 static inline void reset_bdev(struct zram *zram) {}; 874 static int read_from_bdev(struct zram *zram, struct bio_vec *bvec, 875 unsigned long entry, struct bio *parent, bool sync) 876 { 877 return -EIO; 878 } 879 880 static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {}; 881 #endif 882 883 #ifdef CONFIG_ZRAM_MEMORY_TRACKING 884 885 static struct dentry *zram_debugfs_root; 886 887 static void zram_debugfs_create(void) 888 { 889 zram_debugfs_root = debugfs_create_dir("zram", NULL); 890 } 891 892 static void zram_debugfs_destroy(void) 893 { 894 debugfs_remove_recursive(zram_debugfs_root); 895 } 896 897 static void zram_accessed(struct zram *zram, u32 index) 898 { 899 zram_clear_flag(zram, index, ZRAM_IDLE); 900 zram->table[index].ac_time = ktime_get_boottime(); 901 } 902 903 static ssize_t read_block_state(struct file *file, char __user *buf, 904 size_t count, loff_t *ppos) 905 { 906 char *kbuf; 907 ssize_t index, written = 0; 908 struct zram *zram = file->private_data; 909 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT; 910 struct timespec64 ts; 911 912 kbuf = kvmalloc(count, GFP_KERNEL); 913 if (!kbuf) 914 return -ENOMEM; 915 916 down_read(&zram->init_lock); 917 if (!init_done(zram)) { 918 up_read(&zram->init_lock); 919 kvfree(kbuf); 920 return -EINVAL; 921 } 922 923 for (index = *ppos; index < nr_pages; index++) { 924 int copied; 925 926 zram_slot_lock(zram, index); 927 if (!zram_allocated(zram, index)) 928 goto next; 929 930 ts = ktime_to_timespec64(zram->table[index].ac_time); 931 copied = snprintf(kbuf + written, count, 932 "%12zd %12lld.%06lu %c%c%c%c\n", 933 index, (s64)ts.tv_sec, 934 ts.tv_nsec / NSEC_PER_USEC, 935 zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.', 936 zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.', 937 zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.', 938 zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.'); 939 940 if (count <= copied) { 941 zram_slot_unlock(zram, index); 942 break; 943 } 944 written += copied; 945 count -= copied; 946 next: 947 zram_slot_unlock(zram, index); 948 *ppos += 1; 949 } 950 951 up_read(&zram->init_lock); 952 if (copy_to_user(buf, kbuf, written)) 953 written = -EFAULT; 954 kvfree(kbuf); 955 956 return written; 957 } 958 959 static const struct file_operations proc_zram_block_state_op = { 960 .open = simple_open, 961 .read = read_block_state, 962 .llseek = default_llseek, 963 }; 964 965 static void zram_debugfs_register(struct zram *zram) 966 { 967 if (!zram_debugfs_root) 968 return; 969 970 zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name, 971 zram_debugfs_root); 972 debugfs_create_file("block_state", 0400, zram->debugfs_dir, 973 zram, &proc_zram_block_state_op); 974 } 975 976 static void zram_debugfs_unregister(struct zram *zram) 977 { 978 debugfs_remove_recursive(zram->debugfs_dir); 979 } 980 #else 981 static void zram_debugfs_create(void) {}; 982 static void zram_debugfs_destroy(void) {}; 983 static void zram_accessed(struct zram *zram, u32 index) 984 { 985 zram_clear_flag(zram, index, ZRAM_IDLE); 986 }; 987 static void zram_debugfs_register(struct zram *zram) {}; 988 static void zram_debugfs_unregister(struct zram *zram) {}; 989 #endif 990 991 /* 992 * We switched to per-cpu streams and this attr is not needed anymore. 993 * However, we will keep it around for some time, because: 994 * a) we may revert per-cpu streams in the future 995 * b) it's visible to user space and we need to follow our 2 years 996 * retirement rule; but we already have a number of 'soon to be 997 * altered' attrs, so max_comp_streams need to wait for the next 998 * layoff cycle. 999 */ 1000 static ssize_t max_comp_streams_show(struct device *dev, 1001 struct device_attribute *attr, char *buf) 1002 { 1003 return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus()); 1004 } 1005 1006 static ssize_t max_comp_streams_store(struct device *dev, 1007 struct device_attribute *attr, const char *buf, size_t len) 1008 { 1009 return len; 1010 } 1011 1012 static ssize_t comp_algorithm_show(struct device *dev, 1013 struct device_attribute *attr, char *buf) 1014 { 1015 size_t sz; 1016 struct zram *zram = dev_to_zram(dev); 1017 1018 down_read(&zram->init_lock); 1019 sz = zcomp_available_show(zram->compressor, buf); 1020 up_read(&zram->init_lock); 1021 1022 return sz; 1023 } 1024 1025 static ssize_t comp_algorithm_store(struct device *dev, 1026 struct device_attribute *attr, const char *buf, size_t len) 1027 { 1028 struct zram *zram = dev_to_zram(dev); 1029 char compressor[ARRAY_SIZE(zram->compressor)]; 1030 size_t sz; 1031 1032 strlcpy(compressor, buf, sizeof(compressor)); 1033 /* ignore trailing newline */ 1034 sz = strlen(compressor); 1035 if (sz > 0 && compressor[sz - 1] == '\n') 1036 compressor[sz - 1] = 0x00; 1037 1038 if (!zcomp_available_algorithm(compressor)) 1039 return -EINVAL; 1040 1041 down_write(&zram->init_lock); 1042 if (init_done(zram)) { 1043 up_write(&zram->init_lock); 1044 pr_info("Can't change algorithm for initialized device\n"); 1045 return -EBUSY; 1046 } 1047 1048 strcpy(zram->compressor, compressor); 1049 up_write(&zram->init_lock); 1050 return len; 1051 } 1052 1053 static ssize_t compact_store(struct device *dev, 1054 struct device_attribute *attr, const char *buf, size_t len) 1055 { 1056 struct zram *zram = dev_to_zram(dev); 1057 1058 down_read(&zram->init_lock); 1059 if (!init_done(zram)) { 1060 up_read(&zram->init_lock); 1061 return -EINVAL; 1062 } 1063 1064 zs_compact(zram->mem_pool); 1065 up_read(&zram->init_lock); 1066 1067 return len; 1068 } 1069 1070 static ssize_t io_stat_show(struct device *dev, 1071 struct device_attribute *attr, char *buf) 1072 { 1073 struct zram *zram = dev_to_zram(dev); 1074 ssize_t ret; 1075 1076 down_read(&zram->init_lock); 1077 ret = scnprintf(buf, PAGE_SIZE, 1078 "%8llu %8llu %8llu %8llu\n", 1079 (u64)atomic64_read(&zram->stats.failed_reads), 1080 (u64)atomic64_read(&zram->stats.failed_writes), 1081 (u64)atomic64_read(&zram->stats.invalid_io), 1082 (u64)atomic64_read(&zram->stats.notify_free)); 1083 up_read(&zram->init_lock); 1084 1085 return ret; 1086 } 1087 1088 static ssize_t mm_stat_show(struct device *dev, 1089 struct device_attribute *attr, char *buf) 1090 { 1091 struct zram *zram = dev_to_zram(dev); 1092 struct zs_pool_stats pool_stats; 1093 u64 orig_size, mem_used = 0; 1094 long max_used; 1095 ssize_t ret; 1096 1097 memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats)); 1098 1099 down_read(&zram->init_lock); 1100 if (init_done(zram)) { 1101 mem_used = zs_get_total_pages(zram->mem_pool); 1102 zs_pool_stats(zram->mem_pool, &pool_stats); 1103 } 1104 1105 orig_size = atomic64_read(&zram->stats.pages_stored); 1106 max_used = atomic_long_read(&zram->stats.max_used_pages); 1107 1108 ret = scnprintf(buf, PAGE_SIZE, 1109 "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n", 1110 orig_size << PAGE_SHIFT, 1111 (u64)atomic64_read(&zram->stats.compr_data_size), 1112 mem_used << PAGE_SHIFT, 1113 zram->limit_pages << PAGE_SHIFT, 1114 max_used << PAGE_SHIFT, 1115 (u64)atomic64_read(&zram->stats.same_pages), 1116 atomic_long_read(&pool_stats.pages_compacted), 1117 (u64)atomic64_read(&zram->stats.huge_pages), 1118 (u64)atomic64_read(&zram->stats.huge_pages_since)); 1119 up_read(&zram->init_lock); 1120 1121 return ret; 1122 } 1123 1124 #ifdef CONFIG_ZRAM_WRITEBACK 1125 #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12))) 1126 static ssize_t bd_stat_show(struct device *dev, 1127 struct device_attribute *attr, char *buf) 1128 { 1129 struct zram *zram = dev_to_zram(dev); 1130 ssize_t ret; 1131 1132 down_read(&zram->init_lock); 1133 ret = scnprintf(buf, PAGE_SIZE, 1134 "%8llu %8llu %8llu\n", 1135 FOUR_K((u64)atomic64_read(&zram->stats.bd_count)), 1136 FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)), 1137 FOUR_K((u64)atomic64_read(&zram->stats.bd_writes))); 1138 up_read(&zram->init_lock); 1139 1140 return ret; 1141 } 1142 #endif 1143 1144 static ssize_t debug_stat_show(struct device *dev, 1145 struct device_attribute *attr, char *buf) 1146 { 1147 int version = 1; 1148 struct zram *zram = dev_to_zram(dev); 1149 ssize_t ret; 1150 1151 down_read(&zram->init_lock); 1152 ret = scnprintf(buf, PAGE_SIZE, 1153 "version: %d\n%8llu %8llu\n", 1154 version, 1155 (u64)atomic64_read(&zram->stats.writestall), 1156 (u64)atomic64_read(&zram->stats.miss_free)); 1157 up_read(&zram->init_lock); 1158 1159 return ret; 1160 } 1161 1162 static DEVICE_ATTR_RO(io_stat); 1163 static DEVICE_ATTR_RO(mm_stat); 1164 #ifdef CONFIG_ZRAM_WRITEBACK 1165 static DEVICE_ATTR_RO(bd_stat); 1166 #endif 1167 static DEVICE_ATTR_RO(debug_stat); 1168 1169 static void zram_meta_free(struct zram *zram, u64 disksize) 1170 { 1171 size_t num_pages = disksize >> PAGE_SHIFT; 1172 size_t index; 1173 1174 /* Free all pages that are still in this zram device */ 1175 for (index = 0; index < num_pages; index++) 1176 zram_free_page(zram, index); 1177 1178 zs_destroy_pool(zram->mem_pool); 1179 vfree(zram->table); 1180 } 1181 1182 static bool zram_meta_alloc(struct zram *zram, u64 disksize) 1183 { 1184 size_t num_pages; 1185 1186 num_pages = disksize >> PAGE_SHIFT; 1187 zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table))); 1188 if (!zram->table) 1189 return false; 1190 1191 zram->mem_pool = zs_create_pool(zram->disk->disk_name); 1192 if (!zram->mem_pool) { 1193 vfree(zram->table); 1194 return false; 1195 } 1196 1197 if (!huge_class_size) 1198 huge_class_size = zs_huge_class_size(zram->mem_pool); 1199 return true; 1200 } 1201 1202 /* 1203 * To protect concurrent access to the same index entry, 1204 * caller should hold this table index entry's bit_spinlock to 1205 * indicate this index entry is accessing. 1206 */ 1207 static void zram_free_page(struct zram *zram, size_t index) 1208 { 1209 unsigned long handle; 1210 1211 #ifdef CONFIG_ZRAM_MEMORY_TRACKING 1212 zram->table[index].ac_time = 0; 1213 #endif 1214 if (zram_test_flag(zram, index, ZRAM_IDLE)) 1215 zram_clear_flag(zram, index, ZRAM_IDLE); 1216 1217 if (zram_test_flag(zram, index, ZRAM_HUGE)) { 1218 zram_clear_flag(zram, index, ZRAM_HUGE); 1219 atomic64_dec(&zram->stats.huge_pages); 1220 } 1221 1222 if (zram_test_flag(zram, index, ZRAM_WB)) { 1223 zram_clear_flag(zram, index, ZRAM_WB); 1224 free_block_bdev(zram, zram_get_element(zram, index)); 1225 goto out; 1226 } 1227 1228 /* 1229 * No memory is allocated for same element filled pages. 1230 * Simply clear same page flag. 1231 */ 1232 if (zram_test_flag(zram, index, ZRAM_SAME)) { 1233 zram_clear_flag(zram, index, ZRAM_SAME); 1234 atomic64_dec(&zram->stats.same_pages); 1235 goto out; 1236 } 1237 1238 handle = zram_get_handle(zram, index); 1239 if (!handle) 1240 return; 1241 1242 zs_free(zram->mem_pool, handle); 1243 1244 atomic64_sub(zram_get_obj_size(zram, index), 1245 &zram->stats.compr_data_size); 1246 out: 1247 atomic64_dec(&zram->stats.pages_stored); 1248 zram_set_handle(zram, index, 0); 1249 zram_set_obj_size(zram, index, 0); 1250 WARN_ON_ONCE(zram->table[index].flags & 1251 ~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB)); 1252 } 1253 1254 static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index, 1255 struct bio *bio, bool partial_io) 1256 { 1257 struct zcomp_strm *zstrm; 1258 unsigned long handle; 1259 unsigned int size; 1260 void *src, *dst; 1261 int ret; 1262 1263 zram_slot_lock(zram, index); 1264 if (zram_test_flag(zram, index, ZRAM_WB)) { 1265 struct bio_vec bvec; 1266 1267 zram_slot_unlock(zram, index); 1268 1269 bvec.bv_page = page; 1270 bvec.bv_len = PAGE_SIZE; 1271 bvec.bv_offset = 0; 1272 return read_from_bdev(zram, &bvec, 1273 zram_get_element(zram, index), 1274 bio, partial_io); 1275 } 1276 1277 handle = zram_get_handle(zram, index); 1278 if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) { 1279 unsigned long value; 1280 void *mem; 1281 1282 value = handle ? zram_get_element(zram, index) : 0; 1283 mem = kmap_atomic(page); 1284 zram_fill_page(mem, PAGE_SIZE, value); 1285 kunmap_atomic(mem); 1286 zram_slot_unlock(zram, index); 1287 return 0; 1288 } 1289 1290 size = zram_get_obj_size(zram, index); 1291 1292 if (size != PAGE_SIZE) 1293 zstrm = zcomp_stream_get(zram->comp); 1294 1295 src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO); 1296 if (size == PAGE_SIZE) { 1297 dst = kmap_atomic(page); 1298 memcpy(dst, src, PAGE_SIZE); 1299 kunmap_atomic(dst); 1300 ret = 0; 1301 } else { 1302 dst = kmap_atomic(page); 1303 ret = zcomp_decompress(zstrm, src, size, dst); 1304 kunmap_atomic(dst); 1305 zcomp_stream_put(zram->comp); 1306 } 1307 zs_unmap_object(zram->mem_pool, handle); 1308 zram_slot_unlock(zram, index); 1309 1310 /* Should NEVER happen. Return bio error if it does. */ 1311 if (WARN_ON(ret)) 1312 pr_err("Decompression failed! err=%d, page=%u\n", ret, index); 1313 1314 return ret; 1315 } 1316 1317 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, 1318 u32 index, int offset, struct bio *bio) 1319 { 1320 int ret; 1321 struct page *page; 1322 1323 page = bvec->bv_page; 1324 if (is_partial_io(bvec)) { 1325 /* Use a temporary buffer to decompress the page */ 1326 page = alloc_page(GFP_NOIO|__GFP_HIGHMEM); 1327 if (!page) 1328 return -ENOMEM; 1329 } 1330 1331 ret = __zram_bvec_read(zram, page, index, bio, is_partial_io(bvec)); 1332 if (unlikely(ret)) 1333 goto out; 1334 1335 if (is_partial_io(bvec)) { 1336 void *src = kmap_atomic(page); 1337 1338 memcpy_to_bvec(bvec, src + offset); 1339 kunmap_atomic(src); 1340 } 1341 out: 1342 if (is_partial_io(bvec)) 1343 __free_page(page); 1344 1345 return ret; 1346 } 1347 1348 static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, 1349 u32 index, struct bio *bio) 1350 { 1351 int ret = 0; 1352 unsigned long alloced_pages; 1353 unsigned long handle = 0; 1354 unsigned int comp_len = 0; 1355 void *src, *dst, *mem; 1356 struct zcomp_strm *zstrm; 1357 struct page *page = bvec->bv_page; 1358 unsigned long element = 0; 1359 enum zram_pageflags flags = 0; 1360 1361 mem = kmap_atomic(page); 1362 if (page_same_filled(mem, &element)) { 1363 kunmap_atomic(mem); 1364 /* Free memory associated with this sector now. */ 1365 flags = ZRAM_SAME; 1366 atomic64_inc(&zram->stats.same_pages); 1367 goto out; 1368 } 1369 kunmap_atomic(mem); 1370 1371 compress_again: 1372 zstrm = zcomp_stream_get(zram->comp); 1373 src = kmap_atomic(page); 1374 ret = zcomp_compress(zstrm, src, &comp_len); 1375 kunmap_atomic(src); 1376 1377 if (unlikely(ret)) { 1378 zcomp_stream_put(zram->comp); 1379 pr_err("Compression failed! err=%d\n", ret); 1380 zs_free(zram->mem_pool, handle); 1381 return ret; 1382 } 1383 1384 if (comp_len >= huge_class_size) 1385 comp_len = PAGE_SIZE; 1386 /* 1387 * handle allocation has 2 paths: 1388 * a) fast path is executed with preemption disabled (for 1389 * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear, 1390 * since we can't sleep; 1391 * b) slow path enables preemption and attempts to allocate 1392 * the page with __GFP_DIRECT_RECLAIM bit set. we have to 1393 * put per-cpu compression stream and, thus, to re-do 1394 * the compression once handle is allocated. 1395 * 1396 * if we have a 'non-null' handle here then we are coming 1397 * from the slow path and handle has already been allocated. 1398 */ 1399 if (!handle) 1400 handle = zs_malloc(zram->mem_pool, comp_len, 1401 __GFP_KSWAPD_RECLAIM | 1402 __GFP_NOWARN | 1403 __GFP_HIGHMEM | 1404 __GFP_MOVABLE); 1405 if (!handle) { 1406 zcomp_stream_put(zram->comp); 1407 atomic64_inc(&zram->stats.writestall); 1408 handle = zs_malloc(zram->mem_pool, comp_len, 1409 GFP_NOIO | __GFP_HIGHMEM | 1410 __GFP_MOVABLE); 1411 if (handle) 1412 goto compress_again; 1413 return -ENOMEM; 1414 } 1415 1416 alloced_pages = zs_get_total_pages(zram->mem_pool); 1417 update_used_max(zram, alloced_pages); 1418 1419 if (zram->limit_pages && alloced_pages > zram->limit_pages) { 1420 zcomp_stream_put(zram->comp); 1421 zs_free(zram->mem_pool, handle); 1422 return -ENOMEM; 1423 } 1424 1425 dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO); 1426 1427 src = zstrm->buffer; 1428 if (comp_len == PAGE_SIZE) 1429 src = kmap_atomic(page); 1430 memcpy(dst, src, comp_len); 1431 if (comp_len == PAGE_SIZE) 1432 kunmap_atomic(src); 1433 1434 zcomp_stream_put(zram->comp); 1435 zs_unmap_object(zram->mem_pool, handle); 1436 atomic64_add(comp_len, &zram->stats.compr_data_size); 1437 out: 1438 /* 1439 * Free memory associated with this sector 1440 * before overwriting unused sectors. 1441 */ 1442 zram_slot_lock(zram, index); 1443 zram_free_page(zram, index); 1444 1445 if (comp_len == PAGE_SIZE) { 1446 zram_set_flag(zram, index, ZRAM_HUGE); 1447 atomic64_inc(&zram->stats.huge_pages); 1448 atomic64_inc(&zram->stats.huge_pages_since); 1449 } 1450 1451 if (flags) { 1452 zram_set_flag(zram, index, flags); 1453 zram_set_element(zram, index, element); 1454 } else { 1455 zram_set_handle(zram, index, handle); 1456 zram_set_obj_size(zram, index, comp_len); 1457 } 1458 zram_slot_unlock(zram, index); 1459 1460 /* Update stats */ 1461 atomic64_inc(&zram->stats.pages_stored); 1462 return ret; 1463 } 1464 1465 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, 1466 u32 index, int offset, struct bio *bio) 1467 { 1468 int ret; 1469 struct page *page = NULL; 1470 struct bio_vec vec; 1471 1472 vec = *bvec; 1473 if (is_partial_io(bvec)) { 1474 void *dst; 1475 /* 1476 * This is a partial IO. We need to read the full page 1477 * before to write the changes. 1478 */ 1479 page = alloc_page(GFP_NOIO|__GFP_HIGHMEM); 1480 if (!page) 1481 return -ENOMEM; 1482 1483 ret = __zram_bvec_read(zram, page, index, bio, true); 1484 if (ret) 1485 goto out; 1486 1487 dst = kmap_atomic(page); 1488 memcpy_from_bvec(dst + offset, bvec); 1489 kunmap_atomic(dst); 1490 1491 vec.bv_page = page; 1492 vec.bv_len = PAGE_SIZE; 1493 vec.bv_offset = 0; 1494 } 1495 1496 ret = __zram_bvec_write(zram, &vec, index, bio); 1497 out: 1498 if (is_partial_io(bvec)) 1499 __free_page(page); 1500 return ret; 1501 } 1502 1503 /* 1504 * zram_bio_discard - handler on discard request 1505 * @index: physical block index in PAGE_SIZE units 1506 * @offset: byte offset within physical block 1507 */ 1508 static void zram_bio_discard(struct zram *zram, u32 index, 1509 int offset, struct bio *bio) 1510 { 1511 size_t n = bio->bi_iter.bi_size; 1512 1513 /* 1514 * zram manages data in physical block size units. Because logical block 1515 * size isn't identical with physical block size on some arch, we 1516 * could get a discard request pointing to a specific offset within a 1517 * certain physical block. Although we can handle this request by 1518 * reading that physiclal block and decompressing and partially zeroing 1519 * and re-compressing and then re-storing it, this isn't reasonable 1520 * because our intent with a discard request is to save memory. So 1521 * skipping this logical block is appropriate here. 1522 */ 1523 if (offset) { 1524 if (n <= (PAGE_SIZE - offset)) 1525 return; 1526 1527 n -= (PAGE_SIZE - offset); 1528 index++; 1529 } 1530 1531 while (n >= PAGE_SIZE) { 1532 zram_slot_lock(zram, index); 1533 zram_free_page(zram, index); 1534 zram_slot_unlock(zram, index); 1535 atomic64_inc(&zram->stats.notify_free); 1536 index++; 1537 n -= PAGE_SIZE; 1538 } 1539 } 1540 1541 /* 1542 * Returns errno if it has some problem. Otherwise return 0 or 1. 1543 * Returns 0 if IO request was done synchronously 1544 * Returns 1 if IO request was successfully submitted. 1545 */ 1546 static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, 1547 int offset, unsigned int op, struct bio *bio) 1548 { 1549 int ret; 1550 1551 if (!op_is_write(op)) { 1552 atomic64_inc(&zram->stats.num_reads); 1553 ret = zram_bvec_read(zram, bvec, index, offset, bio); 1554 flush_dcache_page(bvec->bv_page); 1555 } else { 1556 atomic64_inc(&zram->stats.num_writes); 1557 ret = zram_bvec_write(zram, bvec, index, offset, bio); 1558 } 1559 1560 zram_slot_lock(zram, index); 1561 zram_accessed(zram, index); 1562 zram_slot_unlock(zram, index); 1563 1564 if (unlikely(ret < 0)) { 1565 if (!op_is_write(op)) 1566 atomic64_inc(&zram->stats.failed_reads); 1567 else 1568 atomic64_inc(&zram->stats.failed_writes); 1569 } 1570 1571 return ret; 1572 } 1573 1574 static void __zram_make_request(struct zram *zram, struct bio *bio) 1575 { 1576 int offset; 1577 u32 index; 1578 struct bio_vec bvec; 1579 struct bvec_iter iter; 1580 unsigned long start_time; 1581 1582 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT; 1583 offset = (bio->bi_iter.bi_sector & 1584 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; 1585 1586 switch (bio_op(bio)) { 1587 case REQ_OP_DISCARD: 1588 case REQ_OP_WRITE_ZEROES: 1589 zram_bio_discard(zram, index, offset, bio); 1590 bio_endio(bio); 1591 return; 1592 default: 1593 break; 1594 } 1595 1596 start_time = bio_start_io_acct(bio); 1597 bio_for_each_segment(bvec, bio, iter) { 1598 struct bio_vec bv = bvec; 1599 unsigned int unwritten = bvec.bv_len; 1600 1601 do { 1602 bv.bv_len = min_t(unsigned int, PAGE_SIZE - offset, 1603 unwritten); 1604 if (zram_bvec_rw(zram, &bv, index, offset, 1605 bio_op(bio), bio) < 0) { 1606 bio->bi_status = BLK_STS_IOERR; 1607 break; 1608 } 1609 1610 bv.bv_offset += bv.bv_len; 1611 unwritten -= bv.bv_len; 1612 1613 update_position(&index, &offset, &bv); 1614 } while (unwritten); 1615 } 1616 bio_end_io_acct(bio, start_time); 1617 bio_endio(bio); 1618 } 1619 1620 /* 1621 * Handler function for all zram I/O requests. 1622 */ 1623 static void zram_submit_bio(struct bio *bio) 1624 { 1625 struct zram *zram = bio->bi_bdev->bd_disk->private_data; 1626 1627 if (!valid_io_request(zram, bio->bi_iter.bi_sector, 1628 bio->bi_iter.bi_size)) { 1629 atomic64_inc(&zram->stats.invalid_io); 1630 bio_io_error(bio); 1631 return; 1632 } 1633 1634 __zram_make_request(zram, bio); 1635 } 1636 1637 static void zram_slot_free_notify(struct block_device *bdev, 1638 unsigned long index) 1639 { 1640 struct zram *zram; 1641 1642 zram = bdev->bd_disk->private_data; 1643 1644 atomic64_inc(&zram->stats.notify_free); 1645 if (!zram_slot_trylock(zram, index)) { 1646 atomic64_inc(&zram->stats.miss_free); 1647 return; 1648 } 1649 1650 zram_free_page(zram, index); 1651 zram_slot_unlock(zram, index); 1652 } 1653 1654 static int zram_rw_page(struct block_device *bdev, sector_t sector, 1655 struct page *page, unsigned int op) 1656 { 1657 int offset, ret; 1658 u32 index; 1659 struct zram *zram; 1660 struct bio_vec bv; 1661 unsigned long start_time; 1662 1663 if (PageTransHuge(page)) 1664 return -ENOTSUPP; 1665 zram = bdev->bd_disk->private_data; 1666 1667 if (!valid_io_request(zram, sector, PAGE_SIZE)) { 1668 atomic64_inc(&zram->stats.invalid_io); 1669 ret = -EINVAL; 1670 goto out; 1671 } 1672 1673 index = sector >> SECTORS_PER_PAGE_SHIFT; 1674 offset = (sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; 1675 1676 bv.bv_page = page; 1677 bv.bv_len = PAGE_SIZE; 1678 bv.bv_offset = 0; 1679 1680 start_time = disk_start_io_acct(bdev->bd_disk, SECTORS_PER_PAGE, op); 1681 ret = zram_bvec_rw(zram, &bv, index, offset, op, NULL); 1682 disk_end_io_acct(bdev->bd_disk, op, start_time); 1683 out: 1684 /* 1685 * If I/O fails, just return error(ie, non-zero) without 1686 * calling page_endio. 1687 * It causes resubmit the I/O with bio request by upper functions 1688 * of rw_page(e.g., swap_readpage, __swap_writepage) and 1689 * bio->bi_end_io does things to handle the error 1690 * (e.g., SetPageError, set_page_dirty and extra works). 1691 */ 1692 if (unlikely(ret < 0)) 1693 return ret; 1694 1695 switch (ret) { 1696 case 0: 1697 page_endio(page, op_is_write(op), 0); 1698 break; 1699 case 1: 1700 ret = 0; 1701 break; 1702 default: 1703 WARN_ON(1); 1704 } 1705 return ret; 1706 } 1707 1708 static void zram_reset_device(struct zram *zram) 1709 { 1710 struct zcomp *comp; 1711 u64 disksize; 1712 1713 down_write(&zram->init_lock); 1714 1715 zram->limit_pages = 0; 1716 1717 if (!init_done(zram)) { 1718 up_write(&zram->init_lock); 1719 return; 1720 } 1721 1722 comp = zram->comp; 1723 disksize = zram->disksize; 1724 zram->disksize = 0; 1725 1726 set_capacity_and_notify(zram->disk, 0); 1727 part_stat_set_all(zram->disk->part0, 0); 1728 1729 /* I/O operation under all of CPU are done so let's free */ 1730 zram_meta_free(zram, disksize); 1731 memset(&zram->stats, 0, sizeof(zram->stats)); 1732 zcomp_destroy(comp); 1733 reset_bdev(zram); 1734 1735 up_write(&zram->init_lock); 1736 } 1737 1738 static ssize_t disksize_store(struct device *dev, 1739 struct device_attribute *attr, const char *buf, size_t len) 1740 { 1741 u64 disksize; 1742 struct zcomp *comp; 1743 struct zram *zram = dev_to_zram(dev); 1744 int err; 1745 1746 disksize = memparse(buf, NULL); 1747 if (!disksize) 1748 return -EINVAL; 1749 1750 down_write(&zram->init_lock); 1751 if (init_done(zram)) { 1752 pr_info("Cannot change disksize for initialized device\n"); 1753 err = -EBUSY; 1754 goto out_unlock; 1755 } 1756 1757 disksize = PAGE_ALIGN(disksize); 1758 if (!zram_meta_alloc(zram, disksize)) { 1759 err = -ENOMEM; 1760 goto out_unlock; 1761 } 1762 1763 comp = zcomp_create(zram->compressor); 1764 if (IS_ERR(comp)) { 1765 pr_err("Cannot initialise %s compressing backend\n", 1766 zram->compressor); 1767 err = PTR_ERR(comp); 1768 goto out_free_meta; 1769 } 1770 1771 zram->comp = comp; 1772 zram->disksize = disksize; 1773 set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT); 1774 up_write(&zram->init_lock); 1775 1776 return len; 1777 1778 out_free_meta: 1779 zram_meta_free(zram, disksize); 1780 out_unlock: 1781 up_write(&zram->init_lock); 1782 return err; 1783 } 1784 1785 static ssize_t reset_store(struct device *dev, 1786 struct device_attribute *attr, const char *buf, size_t len) 1787 { 1788 int ret; 1789 unsigned short do_reset; 1790 struct zram *zram; 1791 struct block_device *bdev; 1792 1793 ret = kstrtou16(buf, 10, &do_reset); 1794 if (ret) 1795 return ret; 1796 1797 if (!do_reset) 1798 return -EINVAL; 1799 1800 zram = dev_to_zram(dev); 1801 bdev = zram->disk->part0; 1802 1803 mutex_lock(&bdev->bd_disk->open_mutex); 1804 /* Do not reset an active device or claimed device */ 1805 if (bdev->bd_openers || zram->claim) { 1806 mutex_unlock(&bdev->bd_disk->open_mutex); 1807 return -EBUSY; 1808 } 1809 1810 /* From now on, anyone can't open /dev/zram[0-9] */ 1811 zram->claim = true; 1812 mutex_unlock(&bdev->bd_disk->open_mutex); 1813 1814 /* Make sure all the pending I/O are finished */ 1815 sync_blockdev(bdev); 1816 zram_reset_device(zram); 1817 1818 mutex_lock(&bdev->bd_disk->open_mutex); 1819 zram->claim = false; 1820 mutex_unlock(&bdev->bd_disk->open_mutex); 1821 1822 return len; 1823 } 1824 1825 static int zram_open(struct block_device *bdev, fmode_t mode) 1826 { 1827 int ret = 0; 1828 struct zram *zram; 1829 1830 WARN_ON(!mutex_is_locked(&bdev->bd_disk->open_mutex)); 1831 1832 zram = bdev->bd_disk->private_data; 1833 /* zram was claimed to reset so open request fails */ 1834 if (zram->claim) 1835 ret = -EBUSY; 1836 1837 return ret; 1838 } 1839 1840 static const struct block_device_operations zram_devops = { 1841 .open = zram_open, 1842 .submit_bio = zram_submit_bio, 1843 .swap_slot_free_notify = zram_slot_free_notify, 1844 .rw_page = zram_rw_page, 1845 .owner = THIS_MODULE 1846 }; 1847 1848 #ifdef CONFIG_ZRAM_WRITEBACK 1849 static const struct block_device_operations zram_wb_devops = { 1850 .open = zram_open, 1851 .submit_bio = zram_submit_bio, 1852 .swap_slot_free_notify = zram_slot_free_notify, 1853 .owner = THIS_MODULE 1854 }; 1855 #endif 1856 1857 static DEVICE_ATTR_WO(compact); 1858 static DEVICE_ATTR_RW(disksize); 1859 static DEVICE_ATTR_RO(initstate); 1860 static DEVICE_ATTR_WO(reset); 1861 static DEVICE_ATTR_WO(mem_limit); 1862 static DEVICE_ATTR_WO(mem_used_max); 1863 static DEVICE_ATTR_WO(idle); 1864 static DEVICE_ATTR_RW(max_comp_streams); 1865 static DEVICE_ATTR_RW(comp_algorithm); 1866 #ifdef CONFIG_ZRAM_WRITEBACK 1867 static DEVICE_ATTR_RW(backing_dev); 1868 static DEVICE_ATTR_WO(writeback); 1869 static DEVICE_ATTR_RW(writeback_limit); 1870 static DEVICE_ATTR_RW(writeback_limit_enable); 1871 #endif 1872 1873 static struct attribute *zram_disk_attrs[] = { 1874 &dev_attr_disksize.attr, 1875 &dev_attr_initstate.attr, 1876 &dev_attr_reset.attr, 1877 &dev_attr_compact.attr, 1878 &dev_attr_mem_limit.attr, 1879 &dev_attr_mem_used_max.attr, 1880 &dev_attr_idle.attr, 1881 &dev_attr_max_comp_streams.attr, 1882 &dev_attr_comp_algorithm.attr, 1883 #ifdef CONFIG_ZRAM_WRITEBACK 1884 &dev_attr_backing_dev.attr, 1885 &dev_attr_writeback.attr, 1886 &dev_attr_writeback_limit.attr, 1887 &dev_attr_writeback_limit_enable.attr, 1888 #endif 1889 &dev_attr_io_stat.attr, 1890 &dev_attr_mm_stat.attr, 1891 #ifdef CONFIG_ZRAM_WRITEBACK 1892 &dev_attr_bd_stat.attr, 1893 #endif 1894 &dev_attr_debug_stat.attr, 1895 NULL, 1896 }; 1897 1898 ATTRIBUTE_GROUPS(zram_disk); 1899 1900 /* 1901 * Allocate and initialize new zram device. the function returns 1902 * '>= 0' device_id upon success, and negative value otherwise. 1903 */ 1904 static int zram_add(void) 1905 { 1906 struct zram *zram; 1907 int ret, device_id; 1908 1909 zram = kzalloc(sizeof(struct zram), GFP_KERNEL); 1910 if (!zram) 1911 return -ENOMEM; 1912 1913 ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL); 1914 if (ret < 0) 1915 goto out_free_dev; 1916 device_id = ret; 1917 1918 init_rwsem(&zram->init_lock); 1919 #ifdef CONFIG_ZRAM_WRITEBACK 1920 spin_lock_init(&zram->wb_limit_lock); 1921 #endif 1922 1923 /* gendisk structure */ 1924 zram->disk = blk_alloc_disk(NUMA_NO_NODE); 1925 if (!zram->disk) { 1926 pr_err("Error allocating disk structure for device %d\n", 1927 device_id); 1928 ret = -ENOMEM; 1929 goto out_free_idr; 1930 } 1931 1932 zram->disk->major = zram_major; 1933 zram->disk->first_minor = device_id; 1934 zram->disk->minors = 1; 1935 zram->disk->flags |= GENHD_FL_NO_PART; 1936 zram->disk->fops = &zram_devops; 1937 zram->disk->private_data = zram; 1938 snprintf(zram->disk->disk_name, 16, "zram%d", device_id); 1939 1940 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ 1941 set_capacity(zram->disk, 0); 1942 /* zram devices sort of resembles non-rotational disks */ 1943 blk_queue_flag_set(QUEUE_FLAG_NONROT, zram->disk->queue); 1944 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue); 1945 1946 /* 1947 * To ensure that we always get PAGE_SIZE aligned 1948 * and n*PAGE_SIZED sized I/O requests. 1949 */ 1950 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); 1951 blk_queue_logical_block_size(zram->disk->queue, 1952 ZRAM_LOGICAL_BLOCK_SIZE); 1953 blk_queue_io_min(zram->disk->queue, PAGE_SIZE); 1954 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); 1955 zram->disk->queue->limits.discard_granularity = PAGE_SIZE; 1956 blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX); 1957 blk_queue_flag_set(QUEUE_FLAG_DISCARD, zram->disk->queue); 1958 1959 /* 1960 * zram_bio_discard() will clear all logical blocks if logical block 1961 * size is identical with physical block size(PAGE_SIZE). But if it is 1962 * different, we will skip discarding some parts of logical blocks in 1963 * the part of the request range which isn't aligned to physical block 1964 * size. So we can't ensure that all discarded logical blocks are 1965 * zeroed. 1966 */ 1967 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE) 1968 blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX); 1969 1970 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue); 1971 ret = device_add_disk(NULL, zram->disk, zram_disk_groups); 1972 if (ret) 1973 goto out_cleanup_disk; 1974 1975 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor)); 1976 1977 zram_debugfs_register(zram); 1978 pr_info("Added device: %s\n", zram->disk->disk_name); 1979 return device_id; 1980 1981 out_cleanup_disk: 1982 blk_cleanup_disk(zram->disk); 1983 out_free_idr: 1984 idr_remove(&zram_index_idr, device_id); 1985 out_free_dev: 1986 kfree(zram); 1987 return ret; 1988 } 1989 1990 static int zram_remove(struct zram *zram) 1991 { 1992 struct block_device *bdev = zram->disk->part0; 1993 bool claimed; 1994 1995 mutex_lock(&bdev->bd_disk->open_mutex); 1996 if (bdev->bd_openers) { 1997 mutex_unlock(&bdev->bd_disk->open_mutex); 1998 return -EBUSY; 1999 } 2000 2001 claimed = zram->claim; 2002 if (!claimed) 2003 zram->claim = true; 2004 mutex_unlock(&bdev->bd_disk->open_mutex); 2005 2006 zram_debugfs_unregister(zram); 2007 2008 if (claimed) { 2009 /* 2010 * If we were claimed by reset_store(), del_gendisk() will 2011 * wait until reset_store() is done, so nothing need to do. 2012 */ 2013 ; 2014 } else { 2015 /* Make sure all the pending I/O are finished */ 2016 sync_blockdev(bdev); 2017 zram_reset_device(zram); 2018 } 2019 2020 pr_info("Removed device: %s\n", zram->disk->disk_name); 2021 2022 del_gendisk(zram->disk); 2023 2024 /* del_gendisk drains pending reset_store */ 2025 WARN_ON_ONCE(claimed && zram->claim); 2026 2027 /* 2028 * disksize_store() may be called in between zram_reset_device() 2029 * and del_gendisk(), so run the last reset to avoid leaking 2030 * anything allocated with disksize_store() 2031 */ 2032 zram_reset_device(zram); 2033 2034 blk_cleanup_disk(zram->disk); 2035 kfree(zram); 2036 return 0; 2037 } 2038 2039 /* zram-control sysfs attributes */ 2040 2041 /* 2042 * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a 2043 * sense that reading from this file does alter the state of your system -- it 2044 * creates a new un-initialized zram device and returns back this device's 2045 * device_id (or an error code if it fails to create a new device). 2046 */ 2047 static ssize_t hot_add_show(struct class *class, 2048 struct class_attribute *attr, 2049 char *buf) 2050 { 2051 int ret; 2052 2053 mutex_lock(&zram_index_mutex); 2054 ret = zram_add(); 2055 mutex_unlock(&zram_index_mutex); 2056 2057 if (ret < 0) 2058 return ret; 2059 return scnprintf(buf, PAGE_SIZE, "%d\n", ret); 2060 } 2061 static struct class_attribute class_attr_hot_add = 2062 __ATTR(hot_add, 0400, hot_add_show, NULL); 2063 2064 static ssize_t hot_remove_store(struct class *class, 2065 struct class_attribute *attr, 2066 const char *buf, 2067 size_t count) 2068 { 2069 struct zram *zram; 2070 int ret, dev_id; 2071 2072 /* dev_id is gendisk->first_minor, which is `int' */ 2073 ret = kstrtoint(buf, 10, &dev_id); 2074 if (ret) 2075 return ret; 2076 if (dev_id < 0) 2077 return -EINVAL; 2078 2079 mutex_lock(&zram_index_mutex); 2080 2081 zram = idr_find(&zram_index_idr, dev_id); 2082 if (zram) { 2083 ret = zram_remove(zram); 2084 if (!ret) 2085 idr_remove(&zram_index_idr, dev_id); 2086 } else { 2087 ret = -ENODEV; 2088 } 2089 2090 mutex_unlock(&zram_index_mutex); 2091 return ret ? ret : count; 2092 } 2093 static CLASS_ATTR_WO(hot_remove); 2094 2095 static struct attribute *zram_control_class_attrs[] = { 2096 &class_attr_hot_add.attr, 2097 &class_attr_hot_remove.attr, 2098 NULL, 2099 }; 2100 ATTRIBUTE_GROUPS(zram_control_class); 2101 2102 static struct class zram_control_class = { 2103 .name = "zram-control", 2104 .owner = THIS_MODULE, 2105 .class_groups = zram_control_class_groups, 2106 }; 2107 2108 static int zram_remove_cb(int id, void *ptr, void *data) 2109 { 2110 WARN_ON_ONCE(zram_remove(ptr)); 2111 return 0; 2112 } 2113 2114 static void destroy_devices(void) 2115 { 2116 class_unregister(&zram_control_class); 2117 idr_for_each(&zram_index_idr, &zram_remove_cb, NULL); 2118 zram_debugfs_destroy(); 2119 idr_destroy(&zram_index_idr); 2120 unregister_blkdev(zram_major, "zram"); 2121 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); 2122 } 2123 2124 static int __init zram_init(void) 2125 { 2126 int ret; 2127 2128 ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare", 2129 zcomp_cpu_up_prepare, zcomp_cpu_dead); 2130 if (ret < 0) 2131 return ret; 2132 2133 ret = class_register(&zram_control_class); 2134 if (ret) { 2135 pr_err("Unable to register zram-control class\n"); 2136 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); 2137 return ret; 2138 } 2139 2140 zram_debugfs_create(); 2141 zram_major = register_blkdev(0, "zram"); 2142 if (zram_major <= 0) { 2143 pr_err("Unable to get major number\n"); 2144 class_unregister(&zram_control_class); 2145 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); 2146 return -EBUSY; 2147 } 2148 2149 while (num_devices != 0) { 2150 mutex_lock(&zram_index_mutex); 2151 ret = zram_add(); 2152 mutex_unlock(&zram_index_mutex); 2153 if (ret < 0) 2154 goto out_error; 2155 num_devices--; 2156 } 2157 2158 return 0; 2159 2160 out_error: 2161 destroy_devices(); 2162 return ret; 2163 } 2164 2165 static void __exit zram_exit(void) 2166 { 2167 destroy_devices(); 2168 } 2169 2170 module_init(zram_init); 2171 module_exit(zram_exit); 2172 2173 module_param(num_devices, uint, 0); 2174 MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices"); 2175 2176 MODULE_LICENSE("Dual BSD/GPL"); 2177 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); 2178 MODULE_DESCRIPTION("Compressed RAM Block Device"); 2179