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