]>
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_free_page(struct zram *zram, size_t index) |
306b0c95 | 98 | { |
c2344348 | 99 | unsigned long handle = zram->table[index].handle; |
130f315a | 100 | u16 size = zram->table[index].size; |
306b0c95 | 101 | |
fd1a30de | 102 | if (unlikely(!handle)) { |
2e882281 NG |
103 | /* |
104 | * No memory is allocated for zero filled pages. | |
105 | * Simply clear zero page flag. | |
106 | */ | |
f1e3cfff NG |
107 | if (zram_test_flag(zram, index, ZRAM_ZERO)) { |
108 | zram_clear_flag(zram, index, ZRAM_ZERO); | |
d178a07c | 109 | zram->stats.pages_zero--; |
306b0c95 NG |
110 | } |
111 | return; | |
112 | } | |
113 | ||
130f315a | 114 | if (unlikely(size > max_zpage_size)) |
d178a07c | 115 | zram->stats.bad_compress--; |
306b0c95 | 116 | |
fd1a30de | 117 | zs_free(zram->mem_pool, handle); |
306b0c95 | 118 | |
130f315a | 119 | if (size <= PAGE_SIZE / 2) |
d178a07c | 120 | zram->stats.good_compress--; |
306b0c95 | 121 | |
fd1a30de NG |
122 | zram_stat64_sub(zram, &zram->stats.compr_size, |
123 | zram->table[index].size); | |
d178a07c | 124 | zram->stats.pages_stored--; |
306b0c95 | 125 | |
c2344348 | 126 | zram->table[index].handle = 0; |
fd1a30de | 127 | zram->table[index].size = 0; |
306b0c95 NG |
128 | } |
129 | ||
924bd88d | 130 | static void handle_zero_page(struct bio_vec *bvec) |
306b0c95 | 131 | { |
924bd88d | 132 | struct page *page = bvec->bv_page; |
306b0c95 | 133 | void *user_mem; |
306b0c95 | 134 | |
ba82fe2e | 135 | user_mem = kmap_atomic(page); |
924bd88d | 136 | memset(user_mem + bvec->bv_offset, 0, bvec->bv_len); |
ba82fe2e | 137 | kunmap_atomic(user_mem); |
306b0c95 | 138 | |
30fb8a71 | 139 | flush_dcache_page(page); |
306b0c95 NG |
140 | } |
141 | ||
924bd88d JM |
142 | static inline int is_partial_io(struct bio_vec *bvec) |
143 | { | |
144 | return bvec->bv_len != PAGE_SIZE; | |
145 | } | |
146 | ||
37b51fdd | 147 | static int zram_decompress_page(struct zram *zram, char *mem, u32 index) |
306b0c95 | 148 | { |
37b51fdd SS |
149 | int ret = LZO_E_OK; |
150 | size_t clen = PAGE_SIZE; | |
151 | unsigned char *cmem; | |
152 | unsigned long handle = zram->table[index].handle; | |
306b0c95 | 153 | |
37b51fdd SS |
154 | if (!handle || zram_test_flag(zram, index, ZRAM_ZERO)) { |
155 | memset(mem, 0, PAGE_SIZE); | |
8c921b2b JM |
156 | return 0; |
157 | } | |
306b0c95 | 158 | |
37b51fdd SS |
159 | cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_RO); |
160 | if (zram->table[index].size == PAGE_SIZE) | |
161 | memcpy(mem, cmem, PAGE_SIZE); | |
162 | else | |
c8f2f0db | 163 | ret = lzo1x_decompress_safe(cmem, zram->table[index].size, |
37b51fdd SS |
164 | mem, &clen); |
165 | zs_unmap_object(zram->mem_pool, handle); | |
a1dd52af | 166 | |
8c921b2b JM |
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; | |
a1dd52af | 172 | } |
306b0c95 | 173 | |
8c921b2b | 174 | return 0; |
306b0c95 NG |
175 | } |
176 | ||
37b51fdd SS |
177 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
178 | u32 index, int offset, struct bio *bio) | |
924bd88d JM |
179 | { |
180 | int ret; | |
37b51fdd SS |
181 | struct page *page; |
182 | unsigned char *user_mem, *uncmem = NULL; | |
924bd88d | 183 | |
37b51fdd SS |
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); | |
924bd88d JM |
189 | return 0; |
190 | } | |
191 | ||
37b51fdd SS |
192 | if (is_partial_io(bvec)) |
193 | /* Use a temporary buffer to decompress the page */ | |
7e5a5104 MK |
194 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
195 | ||
196 | user_mem = kmap_atomic(page); | |
197 | if (!is_partial_io(bvec)) | |
37b51fdd SS |
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 | } | |
924bd88d | 205 | |
37b51fdd | 206 | ret = zram_decompress_page(zram, uncmem, index); |
924bd88d JM |
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); | |
37b51fdd | 211 | goto out_cleanup; |
924bd88d JM |
212 | } |
213 | ||
37b51fdd SS |
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; | |
924bd88d JM |
225 | } |
226 | ||
227 | static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, | |
228 | int offset) | |
306b0c95 | 229 | { |
397c6066 | 230 | int ret = 0; |
8c921b2b | 231 | size_t clen; |
c2344348 | 232 | unsigned long handle; |
130f315a | 233 | struct page *page; |
924bd88d | 234 | unsigned char *user_mem, *cmem, *src, *uncmem = NULL; |
306b0c95 | 235 | |
8c921b2b JM |
236 | page = bvec->bv_page; |
237 | src = zram->compress_buffer; | |
306b0c95 | 238 | |
924bd88d JM |
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 | */ | |
7e5a5104 | 244 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
924bd88d JM |
245 | if (!uncmem) { |
246 | pr_info("Error allocating temp memory!\n"); | |
247 | ret = -ENOMEM; | |
248 | goto out; | |
249 | } | |
37b51fdd | 250 | ret = zram_decompress_page(zram, uncmem, index); |
397c6066 | 251 | if (ret) |
924bd88d | 252 | goto out; |
924bd88d JM |
253 | } |
254 | ||
8c921b2b JM |
255 | /* |
256 | * System overwrites unused sectors. Free memory associated | |
257 | * with this sector now. | |
258 | */ | |
fd1a30de | 259 | if (zram->table[index].handle || |
8c921b2b JM |
260 | zram_test_flag(zram, index, ZRAM_ZERO)) |
261 | zram_free_page(zram, index); | |
306b0c95 | 262 | |
ba82fe2e | 263 | user_mem = kmap_atomic(page); |
924bd88d | 264 | |
397c6066 | 265 | if (is_partial_io(bvec)) { |
924bd88d JM |
266 | memcpy(uncmem + offset, user_mem + bvec->bv_offset, |
267 | bvec->bv_len); | |
397c6066 NG |
268 | kunmap_atomic(user_mem); |
269 | user_mem = NULL; | |
270 | } else { | |
924bd88d | 271 | uncmem = user_mem; |
397c6066 | 272 | } |
924bd88d JM |
273 | |
274 | if (page_zero_filled(uncmem)) { | |
ba82fe2e | 275 | kunmap_atomic(user_mem); |
924bd88d JM |
276 | if (is_partial_io(bvec)) |
277 | kfree(uncmem); | |
d178a07c | 278 | zram->stats.pages_zero++; |
8c921b2b | 279 | zram_set_flag(zram, index, ZRAM_ZERO); |
924bd88d JM |
280 | ret = 0; |
281 | goto out; | |
8c921b2b | 282 | } |
306b0c95 | 283 | |
924bd88d | 284 | ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen, |
8c921b2b | 285 | zram->compress_workmem); |
306b0c95 | 286 | |
397c6066 NG |
287 | if (!is_partial_io(bvec)) { |
288 | kunmap_atomic(user_mem); | |
289 | user_mem = NULL; | |
290 | uncmem = NULL; | |
291 | } | |
306b0c95 | 292 | |
8c921b2b | 293 | if (unlikely(ret != LZO_E_OK)) { |
8c921b2b | 294 | pr_err("Compression failed! err=%d\n", ret); |
924bd88d | 295 | goto out; |
8c921b2b | 296 | } |
306b0c95 | 297 | |
c8f2f0db | 298 | if (unlikely(clen > max_zpage_size)) { |
d178a07c | 299 | zram->stats.bad_compress++; |
c8f2f0db | 300 | clen = PAGE_SIZE; |
397c6066 NG |
301 | src = NULL; |
302 | if (is_partial_io(bvec)) | |
303 | src = uncmem; | |
c8f2f0db | 304 | } |
a1dd52af | 305 | |
130f315a | 306 | handle = zs_malloc(zram->mem_pool, clen); |
fd1a30de | 307 | if (!handle) { |
8c921b2b JM |
308 | pr_info("Error allocating memory for compressed " |
309 | "page: %u, size=%zu\n", index, clen); | |
924bd88d JM |
310 | ret = -ENOMEM; |
311 | goto out; | |
8c921b2b | 312 | } |
b7418510 | 313 | cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_WO); |
306b0c95 | 314 | |
397c6066 NG |
315 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) |
316 | src = kmap_atomic(page); | |
8c921b2b | 317 | memcpy(cmem, src, clen); |
397c6066 NG |
318 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) |
319 | kunmap_atomic(src); | |
306b0c95 | 320 | |
130f315a | 321 | zs_unmap_object(zram->mem_pool, handle); |
fd1a30de NG |
322 | |
323 | zram->table[index].handle = handle; | |
324 | zram->table[index].size = clen; | |
306b0c95 | 325 | |
8c921b2b JM |
326 | /* Update stats */ |
327 | zram_stat64_add(zram, &zram->stats.compr_size, clen); | |
d178a07c | 328 | zram->stats.pages_stored++; |
8c921b2b | 329 | if (clen <= PAGE_SIZE / 2) |
d178a07c | 330 | zram->stats.good_compress++; |
306b0c95 | 331 | |
924bd88d | 332 | out: |
397c6066 NG |
333 | if (is_partial_io(bvec)) |
334 | kfree(uncmem); | |
335 | ||
924bd88d JM |
336 | if (ret) |
337 | zram_stat64_inc(zram, &zram->stats.failed_writes); | |
338 | return ret; | |
8c921b2b JM |
339 | } |
340 | ||
341 | static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, | |
924bd88d | 342 | int offset, struct bio *bio, int rw) |
8c921b2b | 343 | { |
c5bde238 | 344 | int ret; |
8c921b2b | 345 | |
c5bde238 JM |
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; | |
924bd88d JM |
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; | |
8c921b2b JM |
364 | } |
365 | ||
366 | static void __zram_make_request(struct zram *zram, struct bio *bio, int rw) | |
367 | { | |
924bd88d | 368 | int i, offset; |
8c921b2b JM |
369 | u32 index; |
370 | struct bio_vec *bvec; | |
371 | ||
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 | ||
381 | index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT; | |
924bd88d | 382 | offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; |
8c921b2b JM |
383 | |
384 | bio_for_each_segment(bvec, bio, i) { | |
924bd88d JM |
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); | |
a1dd52af | 411 | } |
306b0c95 NG |
412 | |
413 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
414 | bio_endio(bio, 0); | |
7d7854b4 | 415 | return; |
306b0c95 NG |
416 | |
417 | out: | |
306b0c95 | 418 | bio_io_error(bio); |
306b0c95 NG |
419 | } |
420 | ||
306b0c95 | 421 | /* |
924bd88d | 422 | * Check if request is within bounds and aligned on zram logical blocks. |
306b0c95 | 423 | */ |
f1e3cfff | 424 | static inline int valid_io_request(struct zram *zram, struct bio *bio) |
306b0c95 NG |
425 | { |
426 | if (unlikely( | |
f1e3cfff | 427 | (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) || |
924bd88d JM |
428 | (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) || |
429 | (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) { | |
306b0c95 NG |
430 | |
431 | return 0; | |
432 | } | |
433 | ||
a1dd52af | 434 | /* I/O request is valid */ |
306b0c95 NG |
435 | return 1; |
436 | } | |
437 | ||
438 | /* | |
f1e3cfff | 439 | * Handler function for all zram I/O requests. |
306b0c95 | 440 | */ |
5a7bbad2 | 441 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
306b0c95 | 442 | { |
f1e3cfff | 443 | struct zram *zram = queue->queuedata; |
306b0c95 | 444 | |
0900beae JM |
445 | down_read(&zram->init_lock); |
446 | if (unlikely(!zram->init_done)) | |
3de738cd | 447 | goto error; |
0900beae | 448 | |
f1e3cfff NG |
449 | if (!valid_io_request(zram, bio)) { |
450 | zram_stat64_inc(zram, &zram->stats.invalid_io); | |
3de738cd | 451 | goto error; |
6642a67c JM |
452 | } |
453 | ||
8c921b2b | 454 | __zram_make_request(zram, bio, bio_data_dir(bio)); |
0900beae | 455 | up_read(&zram->init_lock); |
306b0c95 | 456 | |
b4fdcb02 | 457 | return; |
0900beae | 458 | |
0900beae | 459 | error: |
3de738cd | 460 | up_read(&zram->init_lock); |
0900beae | 461 | bio_io_error(bio); |
306b0c95 NG |
462 | } |
463 | ||
0900beae | 464 | void __zram_reset_device(struct zram *zram) |
306b0c95 | 465 | { |
97a06382 | 466 | size_t index; |
306b0c95 | 467 | |
0231c403 MK |
468 | if (!zram->init_done) |
469 | return; | |
470 | ||
f1e3cfff | 471 | zram->init_done = 0; |
7eef7533 | 472 | |
306b0c95 | 473 | /* Free various per-device buffers */ |
f1e3cfff NG |
474 | kfree(zram->compress_workmem); |
475 | free_pages((unsigned long)zram->compress_buffer, 1); | |
306b0c95 | 476 | |
f1e3cfff NG |
477 | zram->compress_workmem = NULL; |
478 | zram->compress_buffer = NULL; | |
306b0c95 | 479 | |
f1e3cfff NG |
480 | /* Free all pages that are still in this zram device */ |
481 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { | |
c2344348 | 482 | unsigned long handle = zram->table[index].handle; |
fd1a30de | 483 | if (!handle) |
306b0c95 NG |
484 | continue; |
485 | ||
130f315a | 486 | zs_free(zram->mem_pool, handle); |
306b0c95 NG |
487 | } |
488 | ||
f1e3cfff NG |
489 | vfree(zram->table); |
490 | zram->table = NULL; | |
306b0c95 | 491 | |
fd1a30de | 492 | zs_destroy_pool(zram->mem_pool); |
f1e3cfff | 493 | zram->mem_pool = NULL; |
306b0c95 | 494 | |
306b0c95 | 495 | /* Reset stats */ |
f1e3cfff | 496 | memset(&zram->stats, 0, sizeof(zram->stats)); |
306b0c95 | 497 | |
f1e3cfff | 498 | zram->disksize = 0; |
0231c403 | 499 | set_capacity(zram->disk, 0); |
0900beae JM |
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); | |
306b0c95 NG |
507 | } |
508 | ||
3de738cd | 509 | /* zram->init_lock should be held */ |
33863c21 | 510 | int zram_init_device(struct zram *zram) |
306b0c95 NG |
511 | { |
512 | int ret; | |
513 | size_t num_pages; | |
306b0c95 | 514 | |
0231c403 MK |
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" | |
152bce6b | 522 | "\tMemory Size: %lu kB\n" |
0231c403 MK |
523 | "\tSize you selected: %llu kB\n" |
524 | "Continuing anyway ...\n", | |
525 | (totalram_pages << PAGE_SHIFT) >> 10, zram->disksize >> 10 | |
526 | ); | |
527 | } | |
306b0c95 | 528 | |
f1e3cfff NG |
529 | zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
530 | if (!zram->compress_workmem) { | |
306b0c95 NG |
531 | pr_err("Error allocating compressor working memory!\n"); |
532 | ret = -ENOMEM; | |
5a18c531 | 533 | goto fail_no_table; |
306b0c95 NG |
534 | } |
535 | ||
fb927284 JM |
536 | zram->compress_buffer = |
537 | (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); | |
f1e3cfff | 538 | if (!zram->compress_buffer) { |
306b0c95 NG |
539 | pr_err("Error allocating compressor buffer space\n"); |
540 | ret = -ENOMEM; | |
5a18c531 | 541 | goto fail_no_table; |
306b0c95 NG |
542 | } |
543 | ||
f1e3cfff | 544 | num_pages = zram->disksize >> PAGE_SHIFT; |
5b84cc78 | 545 | zram->table = vzalloc(num_pages * sizeof(*zram->table)); |
f1e3cfff NG |
546 | if (!zram->table) { |
547 | pr_err("Error allocating zram address table\n"); | |
306b0c95 | 548 | ret = -ENOMEM; |
5a18c531 | 549 | goto fail_no_table; |
306b0c95 | 550 | } |
306b0c95 | 551 | |
f1e3cfff NG |
552 | /* zram devices sort of resembles non-rotational disks */ |
553 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); | |
306b0c95 | 554 | |
0d145a50 | 555 | zram->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM); |
f1e3cfff | 556 | if (!zram->mem_pool) { |
306b0c95 NG |
557 | pr_err("Error creating memory pool\n"); |
558 | ret = -ENOMEM; | |
559 | goto fail; | |
560 | } | |
561 | ||
f1e3cfff | 562 | zram->init_done = 1; |
306b0c95 NG |
563 | |
564 | pr_debug("Initialization done!\n"); | |
565 | return 0; | |
566 | ||
5a18c531 JM |
567 | fail_no_table: |
568 | /* To prevent accessing table entries during cleanup */ | |
569 | zram->disksize = 0; | |
306b0c95 | 570 | fail: |
0900beae | 571 | __zram_reset_device(zram); |
306b0c95 NG |
572 | pr_err("Initialization failed: err=%d\n", ret); |
573 | return ret; | |
574 | } | |
575 | ||
2ccbec05 NG |
576 | static void zram_slot_free_notify(struct block_device *bdev, |
577 | unsigned long index) | |
107c161b | 578 | { |
f1e3cfff | 579 | struct zram *zram; |
107c161b | 580 | |
f1e3cfff NG |
581 | zram = bdev->bd_disk->private_data; |
582 | zram_free_page(zram, index); | |
583 | zram_stat64_inc(zram, &zram->stats.notify_free); | |
107c161b NG |
584 | } |
585 | ||
f1e3cfff | 586 | static const struct block_device_operations zram_devops = { |
f1e3cfff | 587 | .swap_slot_free_notify = zram_slot_free_notify, |
107c161b | 588 | .owner = THIS_MODULE |
306b0c95 NG |
589 | }; |
590 | ||
f1e3cfff | 591 | static int create_device(struct zram *zram, int device_id) |
306b0c95 | 592 | { |
de1a21a0 NG |
593 | int ret = 0; |
594 | ||
c5bde238 | 595 | init_rwsem(&zram->lock); |
0900beae | 596 | init_rwsem(&zram->init_lock); |
f1e3cfff | 597 | spin_lock_init(&zram->stat64_lock); |
306b0c95 | 598 | |
f1e3cfff NG |
599 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
600 | if (!zram->queue) { | |
306b0c95 NG |
601 | pr_err("Error allocating disk queue for device %d\n", |
602 | device_id); | |
de1a21a0 NG |
603 | ret = -ENOMEM; |
604 | goto out; | |
306b0c95 NG |
605 | } |
606 | ||
f1e3cfff NG |
607 | blk_queue_make_request(zram->queue, zram_make_request); |
608 | zram->queue->queuedata = zram; | |
306b0c95 NG |
609 | |
610 | /* gendisk structure */ | |
f1e3cfff NG |
611 | zram->disk = alloc_disk(1); |
612 | if (!zram->disk) { | |
613 | blk_cleanup_queue(zram->queue); | |
94b8435f | 614 | pr_warn("Error allocating disk structure for device %d\n", |
306b0c95 | 615 | device_id); |
de1a21a0 NG |
616 | ret = -ENOMEM; |
617 | goto out; | |
306b0c95 NG |
618 | } |
619 | ||
f1e3cfff NG |
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); | |
306b0c95 | 626 | |
33863c21 | 627 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
f1e3cfff | 628 | set_capacity(zram->disk, 0); |
5d83d5a0 | 629 | |
a1dd52af NG |
630 | /* |
631 | * To ensure that we always get PAGE_SIZE aligned | |
632 | * and n*PAGE_SIZED sized I/O requests. | |
633 | */ | |
f1e3cfff | 634 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
7b19b8d4 RJ |
635 | blk_queue_logical_block_size(zram->disk->queue, |
636 | ZRAM_LOGICAL_BLOCK_SIZE); | |
f1e3cfff NG |
637 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
638 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); | |
5d83d5a0 | 639 | |
f1e3cfff | 640 | add_disk(zram->disk); |
306b0c95 | 641 | |
33863c21 NG |
642 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
643 | &zram_disk_attr_group); | |
644 | if (ret < 0) { | |
94b8435f | 645 | pr_warn("Error creating sysfs group"); |
33863c21 NG |
646 | goto out; |
647 | } | |
33863c21 | 648 | |
f1e3cfff | 649 | zram->init_done = 0; |
de1a21a0 NG |
650 | |
651 | out: | |
652 | return ret; | |
306b0c95 NG |
653 | } |
654 | ||
f1e3cfff | 655 | static void destroy_device(struct zram *zram) |
306b0c95 | 656 | { |
33863c21 NG |
657 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
658 | &zram_disk_attr_group); | |
33863c21 | 659 | |
f1e3cfff NG |
660 | if (zram->disk) { |
661 | del_gendisk(zram->disk); | |
662 | put_disk(zram->disk); | |
306b0c95 NG |
663 | } |
664 | ||
f1e3cfff NG |
665 | if (zram->queue) |
666 | blk_cleanup_queue(zram->queue); | |
306b0c95 NG |
667 | } |
668 | ||
5fa5a901 NG |
669 | unsigned int zram_get_num_devices(void) |
670 | { | |
671 | return num_devices; | |
672 | } | |
673 | ||
f1e3cfff | 674 | static int __init zram_init(void) |
306b0c95 | 675 | { |
de1a21a0 | 676 | int ret, dev_id; |
306b0c95 | 677 | |
5fa5a901 | 678 | if (num_devices > max_num_devices) { |
94b8435f | 679 | pr_warn("Invalid value for num_devices: %u\n", |
5fa5a901 | 680 | num_devices); |
de1a21a0 NG |
681 | ret = -EINVAL; |
682 | goto out; | |
306b0c95 NG |
683 | } |
684 | ||
f1e3cfff NG |
685 | zram_major = register_blkdev(0, "zram"); |
686 | if (zram_major <= 0) { | |
94b8435f | 687 | pr_warn("Unable to get major number\n"); |
de1a21a0 NG |
688 | ret = -EBUSY; |
689 | goto out; | |
306b0c95 NG |
690 | } |
691 | ||
306b0c95 | 692 | /* Allocate the device array and initialize each one */ |
5fa5a901 | 693 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
43801f6e | 694 | if (!zram_devices) { |
de1a21a0 NG |
695 | ret = -ENOMEM; |
696 | goto unregister; | |
697 | } | |
306b0c95 | 698 | |
5fa5a901 | 699 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
43801f6e | 700 | ret = create_device(&zram_devices[dev_id], dev_id); |
de1a21a0 | 701 | if (ret) |
3bf040c7 | 702 | goto free_devices; |
de1a21a0 NG |
703 | } |
704 | ||
ca3d70bd DB |
705 | pr_info("Created %u device(s) ...\n", num_devices); |
706 | ||
306b0c95 | 707 | return 0; |
de1a21a0 | 708 | |
3bf040c7 | 709 | free_devices: |
de1a21a0 | 710 | while (dev_id) |
43801f6e NW |
711 | destroy_device(&zram_devices[--dev_id]); |
712 | kfree(zram_devices); | |
de1a21a0 | 713 | unregister: |
f1e3cfff | 714 | unregister_blkdev(zram_major, "zram"); |
de1a21a0 | 715 | out: |
306b0c95 NG |
716 | return ret; |
717 | } | |
718 | ||
f1e3cfff | 719 | static void __exit zram_exit(void) |
306b0c95 NG |
720 | { |
721 | int i; | |
f1e3cfff | 722 | struct zram *zram; |
306b0c95 | 723 | |
5fa5a901 | 724 | for (i = 0; i < num_devices; i++) { |
43801f6e | 725 | zram = &zram_devices[i]; |
306b0c95 | 726 | |
f1e3cfff | 727 | destroy_device(zram); |
0231c403 | 728 | zram_reset_device(zram); |
306b0c95 NG |
729 | } |
730 | ||
f1e3cfff | 731 | unregister_blkdev(zram_major, "zram"); |
306b0c95 | 732 | |
43801f6e | 733 | kfree(zram_devices); |
306b0c95 NG |
734 | pr_debug("Cleanup done!\n"); |
735 | } | |
736 | ||
5fa5a901 NG |
737 | module_param(num_devices, uint, 0); |
738 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); | |
306b0c95 | 739 | |
f1e3cfff NG |
740 | module_init(zram_init); |
741 | module_exit(zram_exit); | |
306b0c95 NG |
742 | |
743 | MODULE_LICENSE("Dual BSD/GPL"); | |
744 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); | |
f1e3cfff | 745 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |