2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
12 * Project home: http://compcache.googlecode.com
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/lzo.h>
33 #include <linux/string.h>
34 #include <linux/vmalloc.h>
39 static int zram_major
;
40 struct zram
*zram_devices
;
42 /* Module params (documentation at end) */
43 static unsigned int num_devices
= 1;
45 static void zram_stat64_add(struct zram
*zram
, u64
*v
, u64 inc
)
47 spin_lock(&zram
->stat64_lock
);
49 spin_unlock(&zram
->stat64_lock
);
52 static void zram_stat64_sub(struct zram
*zram
, u64
*v
, u64 dec
)
54 spin_lock(&zram
->stat64_lock
);
56 spin_unlock(&zram
->stat64_lock
);
59 static void zram_stat64_inc(struct zram
*zram
, u64
*v
)
61 zram_stat64_add(zram
, v
, 1);
64 static int zram_test_flag(struct zram_meta
*meta
, u32 index
,
65 enum zram_pageflags flag
)
67 return meta
->table
[index
].flags
& BIT(flag
);
70 static void zram_set_flag(struct zram_meta
*meta
, u32 index
,
71 enum zram_pageflags flag
)
73 meta
->table
[index
].flags
|= BIT(flag
);
76 static void zram_clear_flag(struct zram_meta
*meta
, u32 index
,
77 enum zram_pageflags flag
)
79 meta
->table
[index
].flags
&= ~BIT(flag
);
82 static int page_zero_filled(void *ptr
)
87 page
= (unsigned long *)ptr
;
89 for (pos
= 0; pos
!= PAGE_SIZE
/ sizeof(*page
); pos
++) {
97 static void zram_free_page(struct zram
*zram
, size_t index
)
99 struct zram_meta
*meta
= zram
->meta
;
100 unsigned long handle
= meta
->table
[index
].handle
;
101 u16 size
= meta
->table
[index
].size
;
103 if (unlikely(!handle
)) {
105 * No memory is allocated for zero filled pages.
106 * Simply clear zero page flag.
108 if (zram_test_flag(meta
, index
, ZRAM_ZERO
)) {
109 zram_clear_flag(meta
, index
, ZRAM_ZERO
);
110 zram
->stats
.pages_zero
--;
115 if (unlikely(size
> max_zpage_size
))
116 zram
->stats
.bad_compress
--;
118 zs_free(meta
->mem_pool
, handle
);
120 if (size
<= PAGE_SIZE
/ 2)
121 zram
->stats
.good_compress
--;
123 zram_stat64_sub(zram
, &zram
->stats
.compr_size
,
124 meta
->table
[index
].size
);
125 zram
->stats
.pages_stored
--;
127 meta
->table
[index
].handle
= 0;
128 meta
->table
[index
].size
= 0;
131 static void handle_zero_page(struct bio_vec
*bvec
)
133 struct page
*page
= bvec
->bv_page
;
136 user_mem
= kmap_atomic(page
);
137 memset(user_mem
+ bvec
->bv_offset
, 0, bvec
->bv_len
);
138 kunmap_atomic(user_mem
);
140 flush_dcache_page(page
);
143 static inline int is_partial_io(struct bio_vec
*bvec
)
145 return bvec
->bv_len
!= PAGE_SIZE
;
148 static int zram_decompress_page(struct zram
*zram
, char *mem
, u32 index
)
151 size_t clen
= PAGE_SIZE
;
153 struct zram_meta
*meta
= zram
->meta
;
154 unsigned long handle
= meta
->table
[index
].handle
;
156 if (!handle
|| zram_test_flag(meta
, index
, ZRAM_ZERO
)) {
157 memset(mem
, 0, PAGE_SIZE
);
161 cmem
= zs_map_object(meta
->mem_pool
, handle
, ZS_MM_RO
);
162 if (meta
->table
[index
].size
== PAGE_SIZE
)
163 memcpy(mem
, cmem
, PAGE_SIZE
);
165 ret
= lzo1x_decompress_safe(cmem
, meta
->table
[index
].size
,
167 zs_unmap_object(meta
->mem_pool
, handle
);
169 /* Should NEVER happen. Return bio error if it does. */
170 if (unlikely(ret
!= LZO_E_OK
)) {
171 pr_err("Decompression failed! err=%d, page=%u\n", ret
, index
);
172 zram_stat64_inc(zram
, &zram
->stats
.failed_reads
);
179 static int zram_bvec_read(struct zram
*zram
, struct bio_vec
*bvec
,
180 u32 index
, int offset
, struct bio
*bio
)
184 unsigned char *user_mem
, *uncmem
= NULL
;
185 struct zram_meta
*meta
= zram
->meta
;
186 page
= bvec
->bv_page
;
188 if (unlikely(!meta
->table
[index
].handle
) ||
189 zram_test_flag(meta
, index
, ZRAM_ZERO
)) {
190 handle_zero_page(bvec
);
194 if (is_partial_io(bvec
))
195 /* Use a temporary buffer to decompress the page */
196 uncmem
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
198 user_mem
= kmap_atomic(page
);
199 if (!is_partial_io(bvec
))
203 pr_info("Unable to allocate temp memory\n");
208 ret
= zram_decompress_page(zram
, uncmem
, index
);
209 /* Should NEVER happen. Return bio error if it does. */
210 if (unlikely(ret
!= LZO_E_OK
))
213 if (is_partial_io(bvec
))
214 memcpy(user_mem
+ bvec
->bv_offset
, uncmem
+ offset
,
217 flush_dcache_page(page
);
220 kunmap_atomic(user_mem
);
221 if (is_partial_io(bvec
))
226 static int zram_bvec_write(struct zram
*zram
, struct bio_vec
*bvec
, u32 index
,
231 unsigned long handle
;
233 unsigned char *user_mem
, *cmem
, *src
, *uncmem
= NULL
;
234 struct zram_meta
*meta
= zram
->meta
;
236 page
= bvec
->bv_page
;
237 src
= meta
->compress_buffer
;
239 if (is_partial_io(bvec
)) {
241 * This is a partial IO. We need to read the full page
242 * before to write the changes.
244 uncmem
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
249 ret
= zram_decompress_page(zram
, uncmem
, index
);
255 * System overwrites unused sectors. Free memory associated
256 * with this sector now.
258 if (meta
->table
[index
].handle
||
259 zram_test_flag(meta
, index
, ZRAM_ZERO
))
260 zram_free_page(zram
, index
);
262 user_mem
= kmap_atomic(page
);
264 if (is_partial_io(bvec
)) {
265 memcpy(uncmem
+ offset
, user_mem
+ bvec
->bv_offset
,
267 kunmap_atomic(user_mem
);
273 if (page_zero_filled(uncmem
)) {
274 kunmap_atomic(user_mem
);
275 zram
->stats
.pages_zero
++;
276 zram_set_flag(meta
, index
, ZRAM_ZERO
);
281 ret
= lzo1x_1_compress(uncmem
, PAGE_SIZE
, src
, &clen
,
282 meta
->compress_workmem
);
284 if (!is_partial_io(bvec
)) {
285 kunmap_atomic(user_mem
);
290 if (unlikely(ret
!= LZO_E_OK
)) {
291 pr_err("Compression failed! err=%d\n", ret
);
295 if (unlikely(clen
> max_zpage_size
)) {
296 zram
->stats
.bad_compress
++;
299 if (is_partial_io(bvec
))
303 handle
= zs_malloc(meta
->mem_pool
, clen
);
305 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
310 cmem
= zs_map_object(meta
->mem_pool
, handle
, ZS_MM_WO
);
312 if ((clen
== PAGE_SIZE
) && !is_partial_io(bvec
))
313 src
= kmap_atomic(page
);
314 memcpy(cmem
, src
, clen
);
315 if ((clen
== PAGE_SIZE
) && !is_partial_io(bvec
))
318 zs_unmap_object(meta
->mem_pool
, handle
);
320 meta
->table
[index
].handle
= handle
;
321 meta
->table
[index
].size
= clen
;
324 zram_stat64_add(zram
, &zram
->stats
.compr_size
, clen
);
325 zram
->stats
.pages_stored
++;
326 if (clen
<= PAGE_SIZE
/ 2)
327 zram
->stats
.good_compress
++;
330 if (is_partial_io(bvec
))
334 zram_stat64_inc(zram
, &zram
->stats
.failed_writes
);
338 static int zram_bvec_rw(struct zram
*zram
, struct bio_vec
*bvec
, u32 index
,
339 int offset
, struct bio
*bio
, int rw
)
344 down_read(&zram
->lock
);
345 ret
= zram_bvec_read(zram
, bvec
, index
, offset
, bio
);
346 up_read(&zram
->lock
);
348 down_write(&zram
->lock
);
349 ret
= zram_bvec_write(zram
, bvec
, index
, offset
);
350 up_write(&zram
->lock
);
356 static void update_position(u32
*index
, int *offset
, struct bio_vec
*bvec
)
358 if (*offset
+ bvec
->bv_len
>= PAGE_SIZE
)
360 *offset
= (*offset
+ bvec
->bv_len
) % PAGE_SIZE
;
363 static void __zram_make_request(struct zram
*zram
, struct bio
*bio
, int rw
)
367 struct bio_vec
*bvec
;
371 zram_stat64_inc(zram
, &zram
->stats
.num_reads
);
374 zram_stat64_inc(zram
, &zram
->stats
.num_writes
);
378 index
= bio
->bi_sector
>> SECTORS_PER_PAGE_SHIFT
;
379 offset
= (bio
->bi_sector
& (SECTORS_PER_PAGE
- 1)) << SECTOR_SHIFT
;
381 bio_for_each_segment(bvec
, bio
, i
) {
382 int max_transfer_size
= PAGE_SIZE
- offset
;
384 if (bvec
->bv_len
> max_transfer_size
) {
386 * zram_bvec_rw() can only make operation on a single
387 * zram page. Split the bio vector.
391 bv
.bv_page
= bvec
->bv_page
;
392 bv
.bv_len
= max_transfer_size
;
393 bv
.bv_offset
= bvec
->bv_offset
;
395 if (zram_bvec_rw(zram
, &bv
, index
, offset
, bio
, rw
) < 0)
398 bv
.bv_len
= bvec
->bv_len
- max_transfer_size
;
399 bv
.bv_offset
+= max_transfer_size
;
400 if (zram_bvec_rw(zram
, &bv
, index
+1, 0, bio
, rw
) < 0)
403 if (zram_bvec_rw(zram
, bvec
, index
, offset
, bio
, rw
)
407 update_position(&index
, &offset
, bvec
);
410 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
419 * Check if request is within bounds and aligned on zram logical blocks.
421 static inline int valid_io_request(struct zram
*zram
, struct bio
*bio
)
423 u64 start
, end
, bound
;
425 /* unaligned request */
426 if (unlikely(bio
->bi_sector
& (ZRAM_SECTOR_PER_LOGICAL_BLOCK
- 1)))
428 if (unlikely(bio
->bi_size
& (ZRAM_LOGICAL_BLOCK_SIZE
- 1)))
431 start
= bio
->bi_sector
;
432 end
= start
+ (bio
->bi_size
>> SECTOR_SHIFT
);
433 bound
= zram
->disksize
>> SECTOR_SHIFT
;
434 /* out of range range */
435 if (unlikely(start
>= bound
|| end
>= bound
|| start
> end
))
438 /* I/O request is valid */
443 * Handler function for all zram I/O requests.
445 static void zram_make_request(struct request_queue
*queue
, struct bio
*bio
)
447 struct zram
*zram
= queue
->queuedata
;
449 down_read(&zram
->init_lock
);
450 if (unlikely(!zram
->init_done
))
453 if (!valid_io_request(zram
, bio
)) {
454 zram_stat64_inc(zram
, &zram
->stats
.invalid_io
);
458 __zram_make_request(zram
, bio
, bio_data_dir(bio
));
459 up_read(&zram
->init_lock
);
464 up_read(&zram
->init_lock
);
468 static void __zram_reset_device(struct zram
*zram
)
471 struct zram_meta
*meta
;
473 if (!zram
->init_done
)
479 /* Free all pages that are still in this zram device */
480 for (index
= 0; index
< zram
->disksize
>> PAGE_SHIFT
; index
++) {
481 unsigned long handle
= meta
->table
[index
].handle
;
485 zs_free(meta
->mem_pool
, handle
);
488 zram_meta_free(zram
->meta
);
491 memset(&zram
->stats
, 0, sizeof(zram
->stats
));
494 set_capacity(zram
->disk
, 0);
497 void zram_reset_device(struct zram
*zram
)
499 down_write(&zram
->init_lock
);
500 __zram_reset_device(zram
);
501 up_write(&zram
->init_lock
);
504 void zram_meta_free(struct zram_meta
*meta
)
506 zs_destroy_pool(meta
->mem_pool
);
507 kfree(meta
->compress_workmem
);
508 free_pages((unsigned long)meta
->compress_buffer
, 1);
513 struct zram_meta
*zram_meta_alloc(u64 disksize
)
516 struct zram_meta
*meta
= kmalloc(sizeof(*meta
), GFP_KERNEL
);
520 meta
->compress_workmem
= kzalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
521 if (!meta
->compress_workmem
)
524 meta
->compress_buffer
=
525 (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, 1);
526 if (!meta
->compress_buffer
) {
527 pr_err("Error allocating compressor buffer space\n");
531 num_pages
= disksize
>> PAGE_SHIFT
;
532 meta
->table
= vzalloc(num_pages
* sizeof(*meta
->table
));
534 pr_err("Error allocating zram address table\n");
538 meta
->mem_pool
= zs_create_pool(GFP_NOIO
| __GFP_HIGHMEM
);
539 if (!meta
->mem_pool
) {
540 pr_err("Error creating memory pool\n");
549 free_pages((unsigned long)meta
->compress_buffer
, 1);
551 kfree(meta
->compress_workmem
);
559 void zram_init_device(struct zram
*zram
, struct zram_meta
*meta
)
561 if (zram
->disksize
> 2 * (totalram_pages
<< PAGE_SHIFT
)) {
563 "There is little point creating a zram of greater than "
564 "twice the size of memory since we expect a 2:1 compression "
565 "ratio. Note that zram uses about 0.1%% of the size of "
566 "the disk when not in use so a huge zram is "
568 "\tMemory Size: %lu kB\n"
569 "\tSize you selected: %llu kB\n"
570 "Continuing anyway ...\n",
571 (totalram_pages
<< PAGE_SHIFT
) >> 10, zram
->disksize
>> 10
575 /* zram devices sort of resembles non-rotational disks */
576 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zram
->disk
->queue
);
581 pr_debug("Initialization done!\n");
584 static void zram_slot_free_notify(struct block_device
*bdev
,
589 zram
= bdev
->bd_disk
->private_data
;
590 down_write(&zram
->lock
);
591 zram_free_page(zram
, index
);
592 up_write(&zram
->lock
);
593 zram_stat64_inc(zram
, &zram
->stats
.notify_free
);
596 static const struct block_device_operations zram_devops
= {
597 .swap_slot_free_notify
= zram_slot_free_notify
,
601 static int create_device(struct zram
*zram
, int device_id
)
605 init_rwsem(&zram
->lock
);
606 init_rwsem(&zram
->init_lock
);
607 spin_lock_init(&zram
->stat64_lock
);
609 zram
->queue
= blk_alloc_queue(GFP_KERNEL
);
611 pr_err("Error allocating disk queue for device %d\n",
616 blk_queue_make_request(zram
->queue
, zram_make_request
);
617 zram
->queue
->queuedata
= zram
;
619 /* gendisk structure */
620 zram
->disk
= alloc_disk(1);
622 pr_warn("Error allocating disk structure for device %d\n",
627 zram
->disk
->major
= zram_major
;
628 zram
->disk
->first_minor
= device_id
;
629 zram
->disk
->fops
= &zram_devops
;
630 zram
->disk
->queue
= zram
->queue
;
631 zram
->disk
->private_data
= zram
;
632 snprintf(zram
->disk
->disk_name
, 16, "zram%d", device_id
);
634 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
635 set_capacity(zram
->disk
, 0);
638 * To ensure that we always get PAGE_SIZE aligned
639 * and n*PAGE_SIZED sized I/O requests.
641 blk_queue_physical_block_size(zram
->disk
->queue
, PAGE_SIZE
);
642 blk_queue_logical_block_size(zram
->disk
->queue
,
643 ZRAM_LOGICAL_BLOCK_SIZE
);
644 blk_queue_io_min(zram
->disk
->queue
, PAGE_SIZE
);
645 blk_queue_io_opt(zram
->disk
->queue
, PAGE_SIZE
);
647 add_disk(zram
->disk
);
649 ret
= sysfs_create_group(&disk_to_dev(zram
->disk
)->kobj
,
650 &zram_disk_attr_group
);
652 pr_warn("Error creating sysfs group");
660 del_gendisk(zram
->disk
);
661 put_disk(zram
->disk
);
663 blk_cleanup_queue(zram
->queue
);
668 static void destroy_device(struct zram
*zram
)
670 sysfs_remove_group(&disk_to_dev(zram
->disk
)->kobj
,
671 &zram_disk_attr_group
);
674 del_gendisk(zram
->disk
);
675 put_disk(zram
->disk
);
679 blk_cleanup_queue(zram
->queue
);
682 unsigned int zram_get_num_devices(void)
687 static int __init
zram_init(void)
691 if (num_devices
> max_num_devices
) {
692 pr_warn("Invalid value for num_devices: %u\n",
698 zram_major
= register_blkdev(0, "zram");
699 if (zram_major
<= 0) {
700 pr_warn("Unable to get major number\n");
705 /* Allocate the device array and initialize each one */
706 zram_devices
= kzalloc(num_devices
* sizeof(struct zram
), GFP_KERNEL
);
712 for (dev_id
= 0; dev_id
< num_devices
; dev_id
++) {
713 ret
= create_device(&zram_devices
[dev_id
], dev_id
);
718 pr_info("Created %u device(s) ...\n", num_devices
);
724 destroy_device(&zram_devices
[--dev_id
]);
727 unregister_blkdev(zram_major
, "zram");
732 static void __exit
zram_exit(void)
737 for (i
= 0; i
< num_devices
; i
++) {
738 zram
= &zram_devices
[i
];
740 get_disk(zram
->disk
);
741 destroy_device(zram
);
742 zram_reset_device(zram
);
743 put_disk(zram
->disk
);
746 unregister_blkdev(zram_major
, "zram");
749 pr_debug("Cleanup done!\n");
752 module_param(num_devices
, uint
, 0);
753 MODULE_PARM_DESC(num_devices
, "Number of zram devices");
755 module_init(zram_init
);
756 module_exit(zram_exit
);
758 MODULE_LICENSE("Dual BSD/GPL");
759 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
760 MODULE_DESCRIPTION("Compressed RAM Block Device");