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