Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / zram / zram_drv.c
1 /*
2 * Compressed RAM block device
3 *
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
5 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 *
12 * Project home: http://compcache.googlecode.com
13 */
14
15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
18 #ifdef CONFIG_ZRAM_DEBUG
19 #define DEBUG
20 #endif
21
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>
35
36 #include "zram_drv.h"
37
38 /* Globals */
39 static int zram_major;
40 struct zram *zram_devices;
41
42 /* Module params (documentation at end) */
43 static unsigned int num_devices;
44
45 static void zram_stat_inc(u32 *v)
46 {
47 *v = *v + 1;
48 }
49
50 static void zram_stat_dec(u32 *v)
51 {
52 *v = *v - 1;
53 }
54
55 static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc)
56 {
57 spin_lock(&zram->stat64_lock);
58 *v = *v + inc;
59 spin_unlock(&zram->stat64_lock);
60 }
61
62 static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec)
63 {
64 spin_lock(&zram->stat64_lock);
65 *v = *v - dec;
66 spin_unlock(&zram->stat64_lock);
67 }
68
69 static void zram_stat64_inc(struct zram *zram, u64 *v)
70 {
71 zram_stat64_add(zram, v, 1);
72 }
73
74 static int zram_test_flag(struct zram *zram, u32 index,
75 enum zram_pageflags flag)
76 {
77 return zram->table[index].flags & BIT(flag);
78 }
79
80 static void zram_set_flag(struct zram *zram, u32 index,
81 enum zram_pageflags flag)
82 {
83 zram->table[index].flags |= BIT(flag);
84 }
85
86 static void zram_clear_flag(struct zram *zram, u32 index,
87 enum zram_pageflags flag)
88 {
89 zram->table[index].flags &= ~BIT(flag);
90 }
91
92 static int page_zero_filled(void *ptr)
93 {
94 unsigned int pos;
95 unsigned long *page;
96
97 page = (unsigned long *)ptr;
98
99 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
100 if (page[pos])
101 return 0;
102 }
103
104 return 1;
105 }
106
107 static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
108 {
109 if (!zram->disksize) {
110 pr_info(
111 "disk size not provided. You can use disksize_kb module "
112 "param to specify size.\nUsing default: (%u%% of RAM).\n",
113 default_disksize_perc_ram
114 );
115 zram->disksize = default_disksize_perc_ram *
116 (totalram_bytes / 100);
117 }
118
119 if (zram->disksize > 2 * (totalram_bytes)) {
120 pr_info(
121 "There is little point creating a zram of greater than "
122 "twice the size of memory since we expect a 2:1 compression "
123 "ratio. Note that zram uses about 0.1%% of the size of "
124 "the disk when not in use so a huge zram is "
125 "wasteful.\n"
126 "\tMemory Size: %zu kB\n"
127 "\tSize you selected: %llu kB\n"
128 "Continuing anyway ...\n",
129 totalram_bytes >> 10, zram->disksize
130 );
131 }
132
133 zram->disksize &= PAGE_MASK;
134 }
135
136 static void zram_free_page(struct zram *zram, size_t index)
137 {
138 unsigned long handle = zram->table[index].handle;
139 u16 size = zram->table[index].size;
140
141 if (unlikely(!handle)) {
142 /*
143 * No memory is allocated for zero filled pages.
144 * Simply clear zero page flag.
145 */
146 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
147 zram_clear_flag(zram, index, ZRAM_ZERO);
148 zram_stat_dec(&zram->stats.pages_zero);
149 }
150 return;
151 }
152
153 if (unlikely(size > max_zpage_size))
154 zram_stat_dec(&zram->stats.bad_compress);
155
156 zs_free(zram->mem_pool, handle);
157
158 if (size <= PAGE_SIZE / 2)
159 zram_stat_dec(&zram->stats.good_compress);
160
161 zram_stat64_sub(zram, &zram->stats.compr_size,
162 zram->table[index].size);
163 zram_stat_dec(&zram->stats.pages_stored);
164
165 zram->table[index].handle = 0;
166 zram->table[index].size = 0;
167 }
168
169 static void handle_zero_page(struct bio_vec *bvec)
170 {
171 struct page *page = bvec->bv_page;
172 void *user_mem;
173
174 user_mem = kmap_atomic(page);
175 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
176 kunmap_atomic(user_mem);
177
178 flush_dcache_page(page);
179 }
180
181 static inline int is_partial_io(struct bio_vec *bvec)
182 {
183 return bvec->bv_len != PAGE_SIZE;
184 }
185
186 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
187 u32 index, int offset, struct bio *bio)
188 {
189 int ret;
190 size_t clen;
191 struct page *page;
192 unsigned char *user_mem, *cmem, *uncmem = NULL;
193
194 page = bvec->bv_page;
195
196 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
197 handle_zero_page(bvec);
198 return 0;
199 }
200
201 /* Requested page is not present in compressed area */
202 if (unlikely(!zram->table[index].handle)) {
203 pr_debug("Read before write: sector=%lu, size=%u",
204 (ulong)(bio->bi_sector), bio->bi_size);
205 handle_zero_page(bvec);
206 return 0;
207 }
208
209 if (is_partial_io(bvec)) {
210 /* Use a temporary buffer to decompress the page */
211 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
212 if (!uncmem) {
213 pr_info("Error allocating temp memory!\n");
214 return -ENOMEM;
215 }
216 }
217
218 user_mem = kmap_atomic(page);
219 if (!is_partial_io(bvec))
220 uncmem = user_mem;
221 clen = PAGE_SIZE;
222
223 cmem = zs_map_object(zram->mem_pool, zram->table[index].handle,
224 ZS_MM_RO);
225
226 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
227 uncmem, &clen);
228
229 if (is_partial_io(bvec)) {
230 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
231 bvec->bv_len);
232 kfree(uncmem);
233 }
234
235 zs_unmap_object(zram->mem_pool, zram->table[index].handle);
236 kunmap_atomic(user_mem);
237
238 /* Should NEVER happen. Return bio error if it does. */
239 if (unlikely(ret != LZO_E_OK)) {
240 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
241 zram_stat64_inc(zram, &zram->stats.failed_reads);
242 return ret;
243 }
244
245 flush_dcache_page(page);
246
247 return 0;
248 }
249
250 static int zram_read_before_write(struct zram *zram, char *mem, u32 index)
251 {
252 int ret;
253 size_t clen = PAGE_SIZE;
254 unsigned char *cmem;
255 unsigned long handle = zram->table[index].handle;
256
257 if (zram_test_flag(zram, index, ZRAM_ZERO) || !handle) {
258 memset(mem, 0, PAGE_SIZE);
259 return 0;
260 }
261
262 cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
263 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
264 mem, &clen);
265 zs_unmap_object(zram->mem_pool, handle);
266
267 /* Should NEVER happen. Return bio error if it does. */
268 if (unlikely(ret != LZO_E_OK)) {
269 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
270 zram_stat64_inc(zram, &zram->stats.failed_reads);
271 return ret;
272 }
273
274 return 0;
275 }
276
277 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
278 int offset)
279 {
280 int ret;
281 size_t clen;
282 unsigned long handle;
283 struct page *page;
284 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
285
286 page = bvec->bv_page;
287 src = zram->compress_buffer;
288
289 if (is_partial_io(bvec)) {
290 /*
291 * This is a partial IO. We need to read the full page
292 * before to write the changes.
293 */
294 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
295 if (!uncmem) {
296 pr_info("Error allocating temp memory!\n");
297 ret = -ENOMEM;
298 goto out;
299 }
300 ret = zram_read_before_write(zram, uncmem, index);
301 if (ret) {
302 kfree(uncmem);
303 goto out;
304 }
305 }
306
307 /*
308 * System overwrites unused sectors. Free memory associated
309 * with this sector now.
310 */
311 if (zram->table[index].handle ||
312 zram_test_flag(zram, index, ZRAM_ZERO))
313 zram_free_page(zram, index);
314
315 user_mem = kmap_atomic(page);
316
317 if (is_partial_io(bvec))
318 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
319 bvec->bv_len);
320 else
321 uncmem = user_mem;
322
323 if (page_zero_filled(uncmem)) {
324 kunmap_atomic(user_mem);
325 if (is_partial_io(bvec))
326 kfree(uncmem);
327 zram_stat_inc(&zram->stats.pages_zero);
328 zram_set_flag(zram, index, ZRAM_ZERO);
329 ret = 0;
330 goto out;
331 }
332
333 ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
334 zram->compress_workmem);
335
336 kunmap_atomic(user_mem);
337 if (is_partial_io(bvec))
338 kfree(uncmem);
339
340 if (unlikely(ret != LZO_E_OK)) {
341 pr_err("Compression failed! err=%d\n", ret);
342 goto out;
343 }
344
345 if (unlikely(clen > max_zpage_size))
346 zram_stat_inc(&zram->stats.bad_compress);
347
348 handle = zs_malloc(zram->mem_pool, clen);
349 if (!handle) {
350 pr_info("Error allocating memory for compressed "
351 "page: %u, size=%zu\n", index, clen);
352 ret = -ENOMEM;
353 goto out;
354 }
355 cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
356
357 memcpy(cmem, src, clen);
358
359 zs_unmap_object(zram->mem_pool, handle);
360
361 zram->table[index].handle = handle;
362 zram->table[index].size = clen;
363
364 /* Update stats */
365 zram_stat64_add(zram, &zram->stats.compr_size, clen);
366 zram_stat_inc(&zram->stats.pages_stored);
367 if (clen <= PAGE_SIZE / 2)
368 zram_stat_inc(&zram->stats.good_compress);
369
370 return 0;
371
372 out:
373 if (ret)
374 zram_stat64_inc(zram, &zram->stats.failed_writes);
375 return ret;
376 }
377
378 static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
379 int offset, struct bio *bio, int rw)
380 {
381 int ret;
382
383 if (rw == READ) {
384 down_read(&zram->lock);
385 ret = zram_bvec_read(zram, bvec, index, offset, bio);
386 up_read(&zram->lock);
387 } else {
388 down_write(&zram->lock);
389 ret = zram_bvec_write(zram, bvec, index, offset);
390 up_write(&zram->lock);
391 }
392
393 return ret;
394 }
395
396 static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
397 {
398 if (*offset + bvec->bv_len >= PAGE_SIZE)
399 (*index)++;
400 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
401 }
402
403 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
404 {
405 int i, offset;
406 u32 index;
407 struct bio_vec *bvec;
408
409 switch (rw) {
410 case READ:
411 zram_stat64_inc(zram, &zram->stats.num_reads);
412 break;
413 case WRITE:
414 zram_stat64_inc(zram, &zram->stats.num_writes);
415 break;
416 }
417
418 index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
419 offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
420
421 bio_for_each_segment(bvec, bio, i) {
422 int max_transfer_size = PAGE_SIZE - offset;
423
424 if (bvec->bv_len > max_transfer_size) {
425 /*
426 * zram_bvec_rw() can only make operation on a single
427 * zram page. Split the bio vector.
428 */
429 struct bio_vec bv;
430
431 bv.bv_page = bvec->bv_page;
432 bv.bv_len = max_transfer_size;
433 bv.bv_offset = bvec->bv_offset;
434
435 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
436 goto out;
437
438 bv.bv_len = bvec->bv_len - max_transfer_size;
439 bv.bv_offset += max_transfer_size;
440 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
441 goto out;
442 } else
443 if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
444 < 0)
445 goto out;
446
447 update_position(&index, &offset, bvec);
448 }
449
450 set_bit(BIO_UPTODATE, &bio->bi_flags);
451 bio_endio(bio, 0);
452 return;
453
454 out:
455 bio_io_error(bio);
456 }
457
458 /*
459 * Check if request is within bounds and aligned on zram logical blocks.
460 */
461 static inline int valid_io_request(struct zram *zram, struct bio *bio)
462 {
463 if (unlikely(
464 (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
465 (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
466 (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
467
468 return 0;
469 }
470
471 /* I/O request is valid */
472 return 1;
473 }
474
475 /*
476 * Handler function for all zram I/O requests.
477 */
478 static void zram_make_request(struct request_queue *queue, struct bio *bio)
479 {
480 struct zram *zram = queue->queuedata;
481
482 if (unlikely(!zram->init_done) && zram_init_device(zram))
483 goto error;
484
485 down_read(&zram->init_lock);
486 if (unlikely(!zram->init_done))
487 goto error_unlock;
488
489 if (!valid_io_request(zram, bio)) {
490 zram_stat64_inc(zram, &zram->stats.invalid_io);
491 goto error_unlock;
492 }
493
494 __zram_make_request(zram, bio, bio_data_dir(bio));
495 up_read(&zram->init_lock);
496
497 return;
498
499 error_unlock:
500 up_read(&zram->init_lock);
501 error:
502 bio_io_error(bio);
503 }
504
505 void __zram_reset_device(struct zram *zram)
506 {
507 size_t index;
508
509 zram->init_done = 0;
510
511 /* Free various per-device buffers */
512 kfree(zram->compress_workmem);
513 free_pages((unsigned long)zram->compress_buffer, 1);
514
515 zram->compress_workmem = NULL;
516 zram->compress_buffer = NULL;
517
518 /* Free all pages that are still in this zram device */
519 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
520 unsigned long handle = zram->table[index].handle;
521 if (!handle)
522 continue;
523
524 zs_free(zram->mem_pool, handle);
525 }
526
527 vfree(zram->table);
528 zram->table = NULL;
529
530 zs_destroy_pool(zram->mem_pool);
531 zram->mem_pool = NULL;
532
533 /* Reset stats */
534 memset(&zram->stats, 0, sizeof(zram->stats));
535
536 zram->disksize = 0;
537 }
538
539 void zram_reset_device(struct zram *zram)
540 {
541 down_write(&zram->init_lock);
542 __zram_reset_device(zram);
543 up_write(&zram->init_lock);
544 }
545
546 int zram_init_device(struct zram *zram)
547 {
548 int ret;
549 size_t num_pages;
550
551 down_write(&zram->init_lock);
552
553 if (zram->init_done) {
554 up_write(&zram->init_lock);
555 return 0;
556 }
557
558 zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
559
560 zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
561 if (!zram->compress_workmem) {
562 pr_err("Error allocating compressor working memory!\n");
563 ret = -ENOMEM;
564 goto fail_no_table;
565 }
566
567 zram->compress_buffer =
568 (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
569 if (!zram->compress_buffer) {
570 pr_err("Error allocating compressor buffer space\n");
571 ret = -ENOMEM;
572 goto fail_no_table;
573 }
574
575 num_pages = zram->disksize >> PAGE_SHIFT;
576 zram->table = vzalloc(num_pages * sizeof(*zram->table));
577 if (!zram->table) {
578 pr_err("Error allocating zram address table\n");
579 ret = -ENOMEM;
580 goto fail_no_table;
581 }
582
583 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
584
585 /* zram devices sort of resembles non-rotational disks */
586 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
587
588 zram->mem_pool = zs_create_pool("zram", GFP_NOIO | __GFP_HIGHMEM);
589 if (!zram->mem_pool) {
590 pr_err("Error creating memory pool\n");
591 ret = -ENOMEM;
592 goto fail;
593 }
594
595 zram->init_done = 1;
596 up_write(&zram->init_lock);
597
598 pr_debug("Initialization done!\n");
599 return 0;
600
601 fail_no_table:
602 /* To prevent accessing table entries during cleanup */
603 zram->disksize = 0;
604 fail:
605 __zram_reset_device(zram);
606 up_write(&zram->init_lock);
607 pr_err("Initialization failed: err=%d\n", ret);
608 return ret;
609 }
610
611 static void zram_slot_free_notify(struct block_device *bdev,
612 unsigned long index)
613 {
614 struct zram *zram;
615
616 zram = bdev->bd_disk->private_data;
617 zram_free_page(zram, index);
618 zram_stat64_inc(zram, &zram->stats.notify_free);
619 }
620
621 static const struct block_device_operations zram_devops = {
622 .swap_slot_free_notify = zram_slot_free_notify,
623 .owner = THIS_MODULE
624 };
625
626 static int create_device(struct zram *zram, int device_id)
627 {
628 int ret = 0;
629
630 init_rwsem(&zram->lock);
631 init_rwsem(&zram->init_lock);
632 spin_lock_init(&zram->stat64_lock);
633
634 zram->queue = blk_alloc_queue(GFP_KERNEL);
635 if (!zram->queue) {
636 pr_err("Error allocating disk queue for device %d\n",
637 device_id);
638 ret = -ENOMEM;
639 goto out;
640 }
641
642 blk_queue_make_request(zram->queue, zram_make_request);
643 zram->queue->queuedata = zram;
644
645 /* gendisk structure */
646 zram->disk = alloc_disk(1);
647 if (!zram->disk) {
648 blk_cleanup_queue(zram->queue);
649 pr_warn("Error allocating disk structure for device %d\n",
650 device_id);
651 ret = -ENOMEM;
652 goto out;
653 }
654
655 zram->disk->major = zram_major;
656 zram->disk->first_minor = device_id;
657 zram->disk->fops = &zram_devops;
658 zram->disk->queue = zram->queue;
659 zram->disk->private_data = zram;
660 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
661
662 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
663 set_capacity(zram->disk, 0);
664
665 /*
666 * To ensure that we always get PAGE_SIZE aligned
667 * and n*PAGE_SIZED sized I/O requests.
668 */
669 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
670 blk_queue_logical_block_size(zram->disk->queue,
671 ZRAM_LOGICAL_BLOCK_SIZE);
672 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
673 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
674
675 add_disk(zram->disk);
676
677 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
678 &zram_disk_attr_group);
679 if (ret < 0) {
680 pr_warn("Error creating sysfs group");
681 goto out;
682 }
683
684 zram->init_done = 0;
685
686 out:
687 return ret;
688 }
689
690 static void destroy_device(struct zram *zram)
691 {
692 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
693 &zram_disk_attr_group);
694
695 if (zram->disk) {
696 del_gendisk(zram->disk);
697 put_disk(zram->disk);
698 }
699
700 if (zram->queue)
701 blk_cleanup_queue(zram->queue);
702 }
703
704 unsigned int zram_get_num_devices(void)
705 {
706 return num_devices;
707 }
708
709 static int __init zram_init(void)
710 {
711 int ret, dev_id;
712
713 if (num_devices > max_num_devices) {
714 pr_warn("Invalid value for num_devices: %u\n",
715 num_devices);
716 ret = -EINVAL;
717 goto out;
718 }
719
720 zram_major = register_blkdev(0, "zram");
721 if (zram_major <= 0) {
722 pr_warn("Unable to get major number\n");
723 ret = -EBUSY;
724 goto out;
725 }
726
727 if (!num_devices) {
728 pr_info("num_devices not specified. Using default: 1\n");
729 num_devices = 1;
730 }
731
732 /* Allocate the device array and initialize each one */
733 pr_info("Creating %u devices ...\n", num_devices);
734 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
735 if (!zram_devices) {
736 ret = -ENOMEM;
737 goto unregister;
738 }
739
740 for (dev_id = 0; dev_id < num_devices; dev_id++) {
741 ret = create_device(&zram_devices[dev_id], dev_id);
742 if (ret)
743 goto free_devices;
744 }
745
746 return 0;
747
748 free_devices:
749 while (dev_id)
750 destroy_device(&zram_devices[--dev_id]);
751 kfree(zram_devices);
752 unregister:
753 unregister_blkdev(zram_major, "zram");
754 out:
755 return ret;
756 }
757
758 static void __exit zram_exit(void)
759 {
760 int i;
761 struct zram *zram;
762
763 for (i = 0; i < num_devices; i++) {
764 zram = &zram_devices[i];
765
766 destroy_device(zram);
767 if (zram->init_done)
768 zram_reset_device(zram);
769 }
770
771 unregister_blkdev(zram_major, "zram");
772
773 kfree(zram_devices);
774 pr_debug("Cleanup done!\n");
775 }
776
777 module_param(num_devices, uint, 0);
778 MODULE_PARM_DESC(num_devices, "Number of zram devices");
779
780 module_init(zram_init);
781 module_exit(zram_exit);
782
783 MODULE_LICENSE("Dual BSD/GPL");
784 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
785 MODULE_DESCRIPTION("Compressed RAM Block Device");