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