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