2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
5 * 2012, 2013 Minchan Kim
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the licence that better fits your requirements.
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 #ifdef CONFIG_ZRAM_DEBUG
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/bio.h>
25 #include <linux/bitops.h>
26 #include <linux/blkdev.h>
27 #include <linux/buffer_head.h>
28 #include <linux/device.h>
29 #include <linux/genhd.h>
30 #include <linux/highmem.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/vmalloc.h>
34 #include <linux/err.h>
39 static int zram_major;
40 static struct zram *zram_devices;
41 static const char *default_compressor = "lzo";
43 /* Module params (documentation at end) */
44 static unsigned int num_devices = 1;
46 #define ZRAM_ATTR_RO(name) \
47 static ssize_t name##_show(struct device *d, \
48 struct device_attribute *attr, char *b) \
50 struct zram *zram = dev_to_zram(d); \
51 return scnprintf(b, PAGE_SIZE, "%llu\n", \
52 (u64)atomic64_read(&zram->stats.name)); \
54 static DEVICE_ATTR_RO(name);
56 static inline int init_done(struct zram *zram)
58 return zram->meta != NULL;
61 static inline struct zram *dev_to_zram(struct device *dev)
63 return (struct zram *)dev_to_disk(dev)->private_data;
66 static ssize_t disksize_show(struct device *dev,
67 struct device_attribute *attr, char *buf)
69 struct zram *zram = dev_to_zram(dev);
71 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
74 static ssize_t initstate_show(struct device *dev,
75 struct device_attribute *attr, char *buf)
78 struct zram *zram = dev_to_zram(dev);
80 down_read(&zram->init_lock);
81 val = init_done(zram);
82 up_read(&zram->init_lock);
84 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
87 static ssize_t orig_data_size_show(struct device *dev,
88 struct device_attribute *attr, char *buf)
90 struct zram *zram = dev_to_zram(dev);
92 return scnprintf(buf, PAGE_SIZE, "%llu\n",
93 (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
96 static ssize_t mem_used_total_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
100 struct zram *zram = dev_to_zram(dev);
102 down_read(&zram->init_lock);
103 if (init_done(zram)) {
104 struct zram_meta *meta = zram->meta;
105 val = zs_get_total_pages(meta->mem_pool);
107 up_read(&zram->init_lock);
109 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
112 static ssize_t max_comp_streams_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
116 struct zram *zram = dev_to_zram(dev);
118 down_read(&zram->init_lock);
119 val = zram->max_comp_streams;
120 up_read(&zram->init_lock);
122 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
125 static ssize_t mem_limit_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
129 struct zram *zram = dev_to_zram(dev);
131 down_read(&zram->init_lock);
132 val = zram->limit_pages;
133 up_read(&zram->init_lock);
135 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
138 static ssize_t mem_limit_store(struct device *dev,
139 struct device_attribute *attr, const char *buf, size_t len)
143 struct zram *zram = dev_to_zram(dev);
145 limit = memparse(buf, &tmp);
146 if (buf == tmp) /* no chars parsed, invalid input */
149 down_write(&zram->init_lock);
150 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
151 up_write(&zram->init_lock);
156 static ssize_t mem_used_max_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
160 struct zram *zram = dev_to_zram(dev);
162 down_read(&zram->init_lock);
164 val = atomic_long_read(&zram->stats.max_used_pages);
165 up_read(&zram->init_lock);
167 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
170 static ssize_t mem_used_max_store(struct device *dev,
171 struct device_attribute *attr, const char *buf, size_t len)
175 struct zram *zram = dev_to_zram(dev);
177 err = kstrtoul(buf, 10, &val);
181 down_read(&zram->init_lock);
182 if (init_done(zram)) {
183 struct zram_meta *meta = zram->meta;
184 atomic_long_set(&zram->stats.max_used_pages,
185 zs_get_total_pages(meta->mem_pool));
187 up_read(&zram->init_lock);
192 static ssize_t max_comp_streams_store(struct device *dev,
193 struct device_attribute *attr, const char *buf, size_t len)
196 struct zram *zram = dev_to_zram(dev);
199 ret = kstrtoint(buf, 0, &num);
205 down_write(&zram->init_lock);
206 if (init_done(zram)) {
207 if (!zcomp_set_max_streams(zram->comp, num)) {
208 pr_info("Cannot change max compression streams\n");
214 zram->max_comp_streams = num;
217 up_write(&zram->init_lock);
221 static ssize_t comp_algorithm_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
225 struct zram *zram = dev_to_zram(dev);
227 down_read(&zram->init_lock);
228 sz = zcomp_available_show(zram->compressor, buf);
229 up_read(&zram->init_lock);
234 static ssize_t comp_algorithm_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t len)
237 struct zram *zram = dev_to_zram(dev);
238 down_write(&zram->init_lock);
239 if (init_done(zram)) {
240 up_write(&zram->init_lock);
241 pr_info("Can't change algorithm for initialized device\n");
244 strlcpy(zram->compressor, buf, sizeof(zram->compressor));
245 up_write(&zram->init_lock);
249 /* flag operations needs meta->tb_lock */
250 static int zram_test_flag(struct zram_meta *meta, u32 index,
251 enum zram_pageflags flag)
253 return meta->table[index].value & BIT(flag);
256 static void zram_set_flag(struct zram_meta *meta, u32 index,
257 enum zram_pageflags flag)
259 meta->table[index].value |= BIT(flag);
262 static void zram_clear_flag(struct zram_meta *meta, u32 index,
263 enum zram_pageflags flag)
265 meta->table[index].value &= ~BIT(flag);
268 static size_t zram_get_obj_size(struct zram_meta *meta, u32 index)
270 return meta->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
273 static void zram_set_obj_size(struct zram_meta *meta,
274 u32 index, size_t size)
276 unsigned long flags = meta->table[index].value >> ZRAM_FLAG_SHIFT;
278 meta->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
281 static inline int is_partial_io(struct bio_vec *bvec)
283 return bvec->bv_len != PAGE_SIZE;
287 * Check if request is within bounds and aligned on zram logical blocks.
289 static inline int valid_io_request(struct zram *zram,
290 sector_t start, unsigned int size)
294 /* unaligned request */
295 if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
297 if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
300 end = start + (size >> SECTOR_SHIFT);
301 bound = zram->disksize >> SECTOR_SHIFT;
302 /* out of range range */
303 if (unlikely(start >= bound || end > bound || start > end))
306 /* I/O request is valid */
310 static void zram_meta_free(struct zram_meta *meta, u64 disksize)
312 size_t num_pages = disksize >> PAGE_SHIFT;
315 /* Free all pages that are still in this zram device */
316 for (index = 0; index < num_pages; index++) {
317 unsigned long handle = meta->table[index].handle;
322 zs_free(meta->mem_pool, handle);
325 zs_destroy_pool(meta->mem_pool);
330 static struct zram_meta *zram_meta_alloc(u64 disksize)
333 struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
338 num_pages = disksize >> PAGE_SHIFT;
339 meta->table = vzalloc(num_pages * sizeof(*meta->table));
341 pr_err("Error allocating zram address table\n");
345 meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
346 if (!meta->mem_pool) {
347 pr_err("Error creating memory pool\n");
359 static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
361 if (*offset + bvec->bv_len >= PAGE_SIZE)
363 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
366 static int page_zero_filled(void *ptr)
371 page = (unsigned long *)ptr;
373 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
381 static void handle_zero_page(struct bio_vec *bvec)
383 struct page *page = bvec->bv_page;
386 user_mem = kmap_atomic(page);
387 if (is_partial_io(bvec))
388 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
390 clear_page(user_mem);
391 kunmap_atomic(user_mem);
393 flush_dcache_page(page);
398 * To protect concurrent access to the same index entry,
399 * caller should hold this table index entry's bit_spinlock to
400 * indicate this index entry is accessing.
402 static void zram_free_page(struct zram *zram, size_t index)
404 struct zram_meta *meta = zram->meta;
405 unsigned long handle = meta->table[index].handle;
407 if (unlikely(!handle)) {
409 * No memory is allocated for zero filled pages.
410 * Simply clear zero page flag.
412 if (zram_test_flag(meta, index, ZRAM_ZERO)) {
413 zram_clear_flag(meta, index, ZRAM_ZERO);
414 atomic64_dec(&zram->stats.zero_pages);
419 zs_free(meta->mem_pool, handle);
421 atomic64_sub(zram_get_obj_size(meta, index),
422 &zram->stats.compr_data_size);
423 atomic64_dec(&zram->stats.pages_stored);
425 meta->table[index].handle = 0;
426 zram_set_obj_size(meta, index, 0);
429 static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
433 struct zram_meta *meta = zram->meta;
434 unsigned long handle;
437 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
438 handle = meta->table[index].handle;
439 size = zram_get_obj_size(meta, index);
441 if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
442 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
447 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
448 if (size == PAGE_SIZE)
449 copy_page(mem, cmem);
451 ret = zcomp_decompress(zram->comp, cmem, size, mem);
452 zs_unmap_object(meta->mem_pool, handle);
453 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
455 /* Should NEVER happen. Return bio error if it does. */
457 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
464 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
465 u32 index, int offset)
469 unsigned char *user_mem, *uncmem = NULL;
470 struct zram_meta *meta = zram->meta;
471 page = bvec->bv_page;
473 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
474 if (unlikely(!meta->table[index].handle) ||
475 zram_test_flag(meta, index, ZRAM_ZERO)) {
476 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
477 handle_zero_page(bvec);
480 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
482 if (is_partial_io(bvec))
483 /* Use a temporary buffer to decompress the page */
484 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
486 user_mem = kmap_atomic(page);
487 if (!is_partial_io(bvec))
491 pr_info("Unable to allocate temp memory\n");
496 ret = zram_decompress_page(zram, uncmem, index);
497 /* Should NEVER happen. Return bio error if it does. */
501 if (is_partial_io(bvec))
502 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
505 flush_dcache_page(page);
508 kunmap_atomic(user_mem);
509 if (is_partial_io(bvec))
514 static inline void update_used_max(struct zram *zram,
515 const unsigned long pages)
517 int old_max, cur_max;
519 old_max = atomic_long_read(&zram->stats.max_used_pages);
524 old_max = atomic_long_cmpxchg(
525 &zram->stats.max_used_pages, cur_max, pages);
526 } while (old_max != cur_max);
529 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
534 unsigned long handle;
536 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
537 struct zram_meta *meta = zram->meta;
538 struct zcomp_strm *zstrm;
540 unsigned long alloced_pages;
542 page = bvec->bv_page;
543 if (is_partial_io(bvec)) {
545 * This is a partial IO. We need to read the full page
546 * before to write the changes.
548 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
553 ret = zram_decompress_page(zram, uncmem, index);
558 zstrm = zcomp_strm_find(zram->comp);
560 user_mem = kmap_atomic(page);
562 if (is_partial_io(bvec)) {
563 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
565 kunmap_atomic(user_mem);
571 if (page_zero_filled(uncmem)) {
573 kunmap_atomic(user_mem);
574 /* Free memory associated with this sector now. */
575 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
576 zram_free_page(zram, index);
577 zram_set_flag(meta, index, ZRAM_ZERO);
578 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
580 atomic64_inc(&zram->stats.zero_pages);
585 ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
586 if (!is_partial_io(bvec)) {
587 kunmap_atomic(user_mem);
593 pr_err("Compression failed! err=%d\n", ret);
597 if (unlikely(clen > max_zpage_size)) {
599 if (is_partial_io(bvec))
603 handle = zs_malloc(meta->mem_pool, clen);
605 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
611 alloced_pages = zs_get_total_pages(meta->mem_pool);
612 if (zram->limit_pages && alloced_pages > zram->limit_pages) {
613 zs_free(meta->mem_pool, handle);
618 update_used_max(zram, alloced_pages);
620 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
622 if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
623 src = kmap_atomic(page);
624 copy_page(cmem, src);
627 memcpy(cmem, src, clen);
630 zcomp_strm_release(zram->comp, zstrm);
632 zs_unmap_object(meta->mem_pool, handle);
635 * Free memory associated with this sector
636 * before overwriting unused sectors.
638 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
639 zram_free_page(zram, index);
641 meta->table[index].handle = handle;
642 zram_set_obj_size(meta, index, clen);
643 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
646 atomic64_add(clen, &zram->stats.compr_data_size);
647 atomic64_inc(&zram->stats.pages_stored);
650 zcomp_strm_release(zram->comp, zstrm);
651 if (is_partial_io(bvec))
656 static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
662 atomic64_inc(&zram->stats.num_reads);
663 ret = zram_bvec_read(zram, bvec, index, offset);
665 atomic64_inc(&zram->stats.num_writes);
666 ret = zram_bvec_write(zram, bvec, index, offset);
671 atomic64_inc(&zram->stats.failed_reads);
673 atomic64_inc(&zram->stats.failed_writes);
680 * zram_bio_discard - handler on discard request
681 * @index: physical block index in PAGE_SIZE units
682 * @offset: byte offset within physical block
684 static void zram_bio_discard(struct zram *zram, u32 index,
685 int offset, struct bio *bio)
687 size_t n = bio->bi_iter.bi_size;
688 struct zram_meta *meta = zram->meta;
691 * zram manages data in physical block size units. Because logical block
692 * size isn't identical with physical block size on some arch, we
693 * could get a discard request pointing to a specific offset within a
694 * certain physical block. Although we can handle this request by
695 * reading that physiclal block and decompressing and partially zeroing
696 * and re-compressing and then re-storing it, this isn't reasonable
697 * because our intent with a discard request is to save memory. So
698 * skipping this logical block is appropriate here.
701 if (n <= (PAGE_SIZE - offset))
704 n -= (PAGE_SIZE - offset);
708 while (n >= PAGE_SIZE) {
709 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
710 zram_free_page(zram, index);
711 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
712 atomic64_inc(&zram->stats.notify_free);
718 static void zram_reset_device(struct zram *zram)
720 down_write(&zram->init_lock);
722 zram->limit_pages = 0;
724 if (!init_done(zram)) {
725 up_write(&zram->init_lock);
729 zcomp_destroy(zram->comp);
730 zram->max_comp_streams = 1;
731 zram_meta_free(zram->meta, zram->disksize);
734 memset(&zram->stats, 0, sizeof(zram->stats));
736 set_capacity(zram->disk, 0);
738 up_write(&zram->init_lock);
741 static ssize_t disksize_store(struct device *dev,
742 struct device_attribute *attr, const char *buf, size_t len)
746 struct zram_meta *meta;
747 struct zram *zram = dev_to_zram(dev);
750 disksize = memparse(buf, NULL);
754 disksize = PAGE_ALIGN(disksize);
755 meta = zram_meta_alloc(disksize);
759 comp = zcomp_create(zram->compressor, zram->max_comp_streams);
761 pr_info("Cannot initialise %s compressing backend\n",
767 down_write(&zram->init_lock);
768 if (init_done(zram)) {
769 pr_info("Cannot change disksize for initialized device\n");
771 goto out_destroy_comp;
776 zram->disksize = disksize;
777 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
778 up_write(&zram->init_lock);
781 * Revalidate disk out of the init_lock to avoid lockdep splat.
782 * It's okay because disk's capacity is protected by init_lock
783 * so that revalidate_disk always sees up-to-date capacity.
785 revalidate_disk(zram->disk);
790 up_write(&zram->init_lock);
793 zram_meta_free(meta, disksize);
797 static ssize_t reset_store(struct device *dev,
798 struct device_attribute *attr, const char *buf, size_t len)
801 unsigned short do_reset;
803 struct block_device *bdev;
805 zram = dev_to_zram(dev);
806 bdev = bdget_disk(zram->disk, 0);
811 mutex_lock(&bdev->bd_mutex);
812 /* Do not reset an active device! */
813 if (bdev->bd_openers) {
818 ret = kstrtou16(buf, 10, &do_reset);
827 /* Make sure all pending I/O is finished */
829 zram_reset_device(zram);
831 mutex_unlock(&bdev->bd_mutex);
832 revalidate_disk(zram->disk);
838 mutex_unlock(&bdev->bd_mutex);
843 static void __zram_make_request(struct zram *zram, struct bio *bio)
848 struct bvec_iter iter;
850 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
851 offset = (bio->bi_iter.bi_sector &
852 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
854 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
855 zram_bio_discard(zram, index, offset, bio);
860 rw = bio_data_dir(bio);
861 bio_for_each_segment(bvec, bio, iter) {
862 int max_transfer_size = PAGE_SIZE - offset;
864 if (bvec.bv_len > max_transfer_size) {
866 * zram_bvec_rw() can only make operation on a single
867 * zram page. Split the bio vector.
871 bv.bv_page = bvec.bv_page;
872 bv.bv_len = max_transfer_size;
873 bv.bv_offset = bvec.bv_offset;
875 if (zram_bvec_rw(zram, &bv, index, offset, rw) < 0)
878 bv.bv_len = bvec.bv_len - max_transfer_size;
879 bv.bv_offset += max_transfer_size;
880 if (zram_bvec_rw(zram, &bv, index + 1, 0, rw) < 0)
883 if (zram_bvec_rw(zram, &bvec, index, offset, rw) < 0)
886 update_position(&index, &offset, &bvec);
889 set_bit(BIO_UPTODATE, &bio->bi_flags);
898 * Handler function for all zram I/O requests.
900 static void zram_make_request(struct request_queue *queue, struct bio *bio)
902 struct zram *zram = queue->queuedata;
904 down_read(&zram->init_lock);
905 if (unlikely(!init_done(zram)))
908 if (!valid_io_request(zram, bio->bi_iter.bi_sector,
909 bio->bi_iter.bi_size)) {
910 atomic64_inc(&zram->stats.invalid_io);
914 __zram_make_request(zram, bio);
915 up_read(&zram->init_lock);
920 up_read(&zram->init_lock);
924 static void zram_slot_free_notify(struct block_device *bdev,
928 struct zram_meta *meta;
930 zram = bdev->bd_disk->private_data;
933 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
934 zram_free_page(zram, index);
935 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
936 atomic64_inc(&zram->stats.notify_free);
939 static int zram_rw_page(struct block_device *bdev, sector_t sector,
940 struct page *page, int rw)
947 zram = bdev->bd_disk->private_data;
948 if (!valid_io_request(zram, sector, PAGE_SIZE)) {
949 atomic64_inc(&zram->stats.invalid_io);
953 down_read(&zram->init_lock);
954 if (unlikely(!init_done(zram))) {
959 index = sector >> SECTORS_PER_PAGE_SHIFT;
960 offset = sector & (SECTORS_PER_PAGE - 1) << SECTOR_SHIFT;
963 bv.bv_len = PAGE_SIZE;
966 err = zram_bvec_rw(zram, &bv, index, offset, rw);
968 up_read(&zram->init_lock);
970 * If I/O fails, just return error(ie, non-zero) without
971 * calling page_endio.
972 * It causes resubmit the I/O with bio request by upper functions
973 * of rw_page(e.g., swap_readpage, __swap_writepage) and
974 * bio->bi_end_io does things to handle the error
975 * (e.g., SetPageError, set_page_dirty and extra works).
978 page_endio(page, rw, 0);
982 static const struct block_device_operations zram_devops = {
983 .swap_slot_free_notify = zram_slot_free_notify,
984 .rw_page = zram_rw_page,
988 static DEVICE_ATTR_RW(disksize);
989 static DEVICE_ATTR_RO(initstate);
990 static DEVICE_ATTR_WO(reset);
991 static DEVICE_ATTR_RO(orig_data_size);
992 static DEVICE_ATTR_RO(mem_used_total);
993 static DEVICE_ATTR_RW(mem_limit);
994 static DEVICE_ATTR_RW(mem_used_max);
995 static DEVICE_ATTR_RW(max_comp_streams);
996 static DEVICE_ATTR_RW(comp_algorithm);
998 ZRAM_ATTR_RO(num_reads);
999 ZRAM_ATTR_RO(num_writes);
1000 ZRAM_ATTR_RO(failed_reads);
1001 ZRAM_ATTR_RO(failed_writes);
1002 ZRAM_ATTR_RO(invalid_io);
1003 ZRAM_ATTR_RO(notify_free);
1004 ZRAM_ATTR_RO(zero_pages);
1005 ZRAM_ATTR_RO(compr_data_size);
1007 static struct attribute *zram_disk_attrs[] = {
1008 &dev_attr_disksize.attr,
1009 &dev_attr_initstate.attr,
1010 &dev_attr_reset.attr,
1011 &dev_attr_num_reads.attr,
1012 &dev_attr_num_writes.attr,
1013 &dev_attr_failed_reads.attr,
1014 &dev_attr_failed_writes.attr,
1015 &dev_attr_invalid_io.attr,
1016 &dev_attr_notify_free.attr,
1017 &dev_attr_zero_pages.attr,
1018 &dev_attr_orig_data_size.attr,
1019 &dev_attr_compr_data_size.attr,
1020 &dev_attr_mem_used_total.attr,
1021 &dev_attr_mem_limit.attr,
1022 &dev_attr_mem_used_max.attr,
1023 &dev_attr_max_comp_streams.attr,
1024 &dev_attr_comp_algorithm.attr,
1028 static struct attribute_group zram_disk_attr_group = {
1029 .attrs = zram_disk_attrs,
1032 static int create_device(struct zram *zram, int device_id)
1036 init_rwsem(&zram->init_lock);
1038 zram->queue = blk_alloc_queue(GFP_KERNEL);
1040 pr_err("Error allocating disk queue for device %d\n",
1045 blk_queue_make_request(zram->queue, zram_make_request);
1046 zram->queue->queuedata = zram;
1048 /* gendisk structure */
1049 zram->disk = alloc_disk(1);
1051 pr_warn("Error allocating disk structure for device %d\n",
1053 goto out_free_queue;
1056 zram->disk->major = zram_major;
1057 zram->disk->first_minor = device_id;
1058 zram->disk->fops = &zram_devops;
1059 zram->disk->queue = zram->queue;
1060 zram->disk->private_data = zram;
1061 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
1063 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
1064 set_capacity(zram->disk, 0);
1065 /* zram devices sort of resembles non-rotational disks */
1066 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
1067 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
1069 * To ensure that we always get PAGE_SIZE aligned
1070 * and n*PAGE_SIZED sized I/O requests.
1072 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
1073 blk_queue_logical_block_size(zram->disk->queue,
1074 ZRAM_LOGICAL_BLOCK_SIZE);
1075 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1076 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
1077 zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1078 zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
1080 * zram_bio_discard() will clear all logical blocks if logical block
1081 * size is identical with physical block size(PAGE_SIZE). But if it is
1082 * different, we will skip discarding some parts of logical blocks in
1083 * the part of the request range which isn't aligned to physical block
1084 * size. So we can't ensure that all discarded logical blocks are
1087 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1088 zram->disk->queue->limits.discard_zeroes_data = 1;
1090 zram->disk->queue->limits.discard_zeroes_data = 0;
1091 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
1093 add_disk(zram->disk);
1095 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
1096 &zram_disk_attr_group);
1098 pr_warn("Error creating sysfs group");
1101 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
1103 zram->max_comp_streams = 1;
1107 del_gendisk(zram->disk);
1108 put_disk(zram->disk);
1110 blk_cleanup_queue(zram->queue);
1115 static void destroy_devices(unsigned int nr)
1120 for (i = 0; i < nr; i++) {
1121 zram = &zram_devices[i];
1123 * Remove sysfs first, so no one will perform a disksize
1124 * store while we destroy the devices
1126 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
1127 &zram_disk_attr_group);
1129 zram_reset_device(zram);
1131 del_gendisk(zram->disk);
1132 put_disk(zram->disk);
1134 blk_cleanup_queue(zram->queue);
1137 kfree(zram_devices);
1138 unregister_blkdev(zram_major, "zram");
1139 pr_info("Destroyed %u device(s)\n", nr);
1142 static int __init zram_init(void)
1146 if (num_devices > max_num_devices) {
1147 pr_warn("Invalid value for num_devices: %u\n",
1152 zram_major = register_blkdev(0, "zram");
1153 if (zram_major <= 0) {
1154 pr_warn("Unable to get major number\n");
1158 /* Allocate the device array and initialize each one */
1159 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
1160 if (!zram_devices) {
1161 unregister_blkdev(zram_major, "zram");
1165 for (dev_id = 0; dev_id < num_devices; dev_id++) {
1166 ret = create_device(&zram_devices[dev_id], dev_id);
1171 pr_info("Created %u device(s)\n", num_devices);
1175 destroy_devices(dev_id);
1179 static void __exit zram_exit(void)
1181 destroy_devices(num_devices);
1184 module_init(zram_init);
1185 module_exit(zram_exit);
1187 module_param(num_devices, uint, 0);
1188 MODULE_PARM_DESC(num_devices, "Number of zram devices");
1190 MODULE_LICENSE("Dual BSD/GPL");
1191 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
1192 MODULE_DESCRIPTION("Compressed RAM Block Device");