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