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; |
0f0e3ba3 | 40 | static 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 | ||
42e99bd9 JL |
131 | static inline int is_partial_io(struct bio_vec *bvec) |
132 | { | |
133 | return bvec->bv_len != PAGE_SIZE; | |
134 | } | |
135 | ||
924bd88d | 136 | static void handle_zero_page(struct bio_vec *bvec) |
306b0c95 | 137 | { |
924bd88d | 138 | struct page *page = bvec->bv_page; |
306b0c95 | 139 | void *user_mem; |
306b0c95 | 140 | |
ba82fe2e | 141 | user_mem = kmap_atomic(page); |
42e99bd9 JL |
142 | if (is_partial_io(bvec)) |
143 | memset(user_mem + bvec->bv_offset, 0, bvec->bv_len); | |
144 | else | |
145 | clear_page(user_mem); | |
ba82fe2e | 146 | kunmap_atomic(user_mem); |
306b0c95 | 147 | |
30fb8a71 | 148 | flush_dcache_page(page); |
306b0c95 NG |
149 | } |
150 | ||
37b51fdd | 151 | static int zram_decompress_page(struct zram *zram, char *mem, u32 index) |
306b0c95 | 152 | { |
37b51fdd SS |
153 | int ret = LZO_E_OK; |
154 | size_t clen = PAGE_SIZE; | |
155 | unsigned char *cmem; | |
8b3cc3ed MK |
156 | struct zram_meta *meta = zram->meta; |
157 | unsigned long handle = meta->table[index].handle; | |
306b0c95 | 158 | |
8b3cc3ed | 159 | if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) { |
42e99bd9 | 160 | clear_page(mem); |
8c921b2b JM |
161 | return 0; |
162 | } | |
306b0c95 | 163 | |
8b3cc3ed MK |
164 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO); |
165 | if (meta->table[index].size == PAGE_SIZE) | |
42e99bd9 | 166 | copy_page(mem, cmem); |
37b51fdd | 167 | else |
8b3cc3ed | 168 | ret = lzo1x_decompress_safe(cmem, meta->table[index].size, |
37b51fdd | 169 | mem, &clen); |
8b3cc3ed | 170 | zs_unmap_object(meta->mem_pool, handle); |
a1dd52af | 171 | |
8c921b2b JM |
172 | /* Should NEVER happen. Return bio error if it does. */ |
173 | if (unlikely(ret != LZO_E_OK)) { | |
174 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); | |
175 | zram_stat64_inc(zram, &zram->stats.failed_reads); | |
176 | return ret; | |
a1dd52af | 177 | } |
306b0c95 | 178 | |
8c921b2b | 179 | return 0; |
306b0c95 NG |
180 | } |
181 | ||
37b51fdd SS |
182 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
183 | u32 index, int offset, struct bio *bio) | |
924bd88d JM |
184 | { |
185 | int ret; | |
37b51fdd SS |
186 | struct page *page; |
187 | unsigned char *user_mem, *uncmem = NULL; | |
8b3cc3ed | 188 | struct zram_meta *meta = zram->meta; |
37b51fdd SS |
189 | page = bvec->bv_page; |
190 | ||
8b3cc3ed MK |
191 | if (unlikely(!meta->table[index].handle) || |
192 | zram_test_flag(meta, index, ZRAM_ZERO)) { | |
37b51fdd | 193 | handle_zero_page(bvec); |
924bd88d JM |
194 | return 0; |
195 | } | |
196 | ||
37b51fdd SS |
197 | if (is_partial_io(bvec)) |
198 | /* Use a temporary buffer to decompress the page */ | |
7e5a5104 MK |
199 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
200 | ||
201 | user_mem = kmap_atomic(page); | |
202 | if (!is_partial_io(bvec)) | |
37b51fdd SS |
203 | uncmem = user_mem; |
204 | ||
205 | if (!uncmem) { | |
206 | pr_info("Unable to allocate temp memory\n"); | |
207 | ret = -ENOMEM; | |
208 | goto out_cleanup; | |
209 | } | |
924bd88d | 210 | |
37b51fdd | 211 | ret = zram_decompress_page(zram, uncmem, index); |
924bd88d | 212 | /* Should NEVER happen. Return bio error if it does. */ |
25eeb667 | 213 | if (unlikely(ret != LZO_E_OK)) |
37b51fdd | 214 | goto out_cleanup; |
924bd88d | 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); |
d178a07c | 278 | zram->stats.pages_zero++; |
8b3cc3ed | 279 | zram_set_flag(meta, 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, |
8b3cc3ed | 285 | meta->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 | |
8b3cc3ed | 306 | handle = zs_malloc(meta->mem_pool, clen); |
fd1a30de | 307 | if (!handle) { |
596b3dd4 MR |
308 | pr_info("Error allocating memory for compressed page: %u, size=%zu\n", |
309 | index, clen); | |
924bd88d JM |
310 | ret = -ENOMEM; |
311 | goto out; | |
8c921b2b | 312 | } |
8b3cc3ed | 313 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO); |
306b0c95 | 314 | |
42e99bd9 | 315 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) { |
397c6066 | 316 | src = kmap_atomic(page); |
42e99bd9 | 317 | copy_page(cmem, src); |
397c6066 | 318 | kunmap_atomic(src); |
42e99bd9 JL |
319 | } else { |
320 | memcpy(cmem, src, clen); | |
321 | } | |
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 | 427 | { |
12a7ad3b JL |
428 | u64 start, end, bound; |
429 | ||
430 | /* unaligned request */ | |
431 | if (unlikely(bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1))) | |
432 | return 0; | |
433 | if (unlikely(bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1))) | |
434 | return 0; | |
306b0c95 | 435 | |
12a7ad3b JL |
436 | start = bio->bi_sector; |
437 | end = start + (bio->bi_size >> SECTOR_SHIFT); | |
438 | bound = zram->disksize >> SECTOR_SHIFT; | |
439 | /* out of range range */ | |
440 | if (unlikely(start >= bound || end >= bound || start > end)) | |
306b0c95 | 441 | return 0; |
306b0c95 | 442 | |
a1dd52af | 443 | /* I/O request is valid */ |
306b0c95 NG |
444 | return 1; |
445 | } | |
446 | ||
447 | /* | |
f1e3cfff | 448 | * Handler function for all zram I/O requests. |
306b0c95 | 449 | */ |
5a7bbad2 | 450 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
306b0c95 | 451 | { |
f1e3cfff | 452 | struct zram *zram = queue->queuedata; |
306b0c95 | 453 | |
0900beae JM |
454 | down_read(&zram->init_lock); |
455 | if (unlikely(!zram->init_done)) | |
3de738cd | 456 | goto error; |
0900beae | 457 | |
f1e3cfff NG |
458 | if (!valid_io_request(zram, bio)) { |
459 | zram_stat64_inc(zram, &zram->stats.invalid_io); | |
3de738cd | 460 | goto error; |
6642a67c JM |
461 | } |
462 | ||
8c921b2b | 463 | __zram_make_request(zram, bio, bio_data_dir(bio)); |
0900beae | 464 | up_read(&zram->init_lock); |
306b0c95 | 465 | |
b4fdcb02 | 466 | return; |
0900beae | 467 | |
0900beae | 468 | error: |
3de738cd | 469 | up_read(&zram->init_lock); |
0900beae | 470 | bio_io_error(bio); |
306b0c95 NG |
471 | } |
472 | ||
1e927711 | 473 | static void __zram_reset_device(struct zram *zram) |
306b0c95 | 474 | { |
97a06382 | 475 | size_t index; |
8b3cc3ed | 476 | struct zram_meta *meta; |
306b0c95 | 477 | |
0231c403 MK |
478 | if (!zram->init_done) |
479 | return; | |
480 | ||
8b3cc3ed | 481 | meta = zram->meta; |
f1e3cfff | 482 | zram->init_done = 0; |
7eef7533 | 483 | |
f1e3cfff NG |
484 | /* Free all pages that are still in this zram device */ |
485 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { | |
8b3cc3ed | 486 | unsigned long handle = meta->table[index].handle; |
fd1a30de | 487 | if (!handle) |
306b0c95 NG |
488 | continue; |
489 | ||
8b3cc3ed | 490 | zs_free(meta->mem_pool, handle); |
306b0c95 NG |
491 | } |
492 | ||
8b3cc3ed MK |
493 | zram_meta_free(zram->meta); |
494 | zram->meta = NULL; | |
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 | ||
8b3cc3ed MK |
509 | void zram_meta_free(struct zram_meta *meta) |
510 | { | |
511 | zs_destroy_pool(meta->mem_pool); | |
512 | kfree(meta->compress_workmem); | |
513 | free_pages((unsigned long)meta->compress_buffer, 1); | |
514 | vfree(meta->table); | |
515 | kfree(meta); | |
516 | } | |
517 | ||
518 | struct zram_meta *zram_meta_alloc(u64 disksize) | |
306b0c95 | 519 | { |
306b0c95 | 520 | size_t num_pages; |
8b3cc3ed MK |
521 | struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL); |
522 | if (!meta) | |
523 | goto out; | |
524 | ||
525 | meta->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); | |
78110bb8 | 526 | if (!meta->compress_workmem) |
8b3cc3ed | 527 | goto free_meta; |
306b0c95 | 528 | |
8b3cc3ed MK |
529 | meta->compress_buffer = |
530 | (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); | |
531 | if (!meta->compress_buffer) { | |
532 | pr_err("Error allocating compressor buffer space\n"); | |
533 | goto free_workmem; | |
534 | } | |
535 | ||
536 | num_pages = disksize >> PAGE_SHIFT; | |
537 | meta->table = vzalloc(num_pages * sizeof(*meta->table)); | |
538 | if (!meta->table) { | |
539 | pr_err("Error allocating zram address table\n"); | |
540 | goto free_buffer; | |
541 | } | |
542 | ||
543 | meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM); | |
544 | if (!meta->mem_pool) { | |
545 | pr_err("Error creating memory pool\n"); | |
546 | goto free_table; | |
547 | } | |
548 | ||
549 | return meta; | |
550 | ||
551 | free_table: | |
552 | vfree(meta->table); | |
553 | free_buffer: | |
554 | free_pages((unsigned long)meta->compress_buffer, 1); | |
555 | free_workmem: | |
556 | kfree(meta->compress_workmem); | |
557 | free_meta: | |
558 | kfree(meta); | |
559 | meta = NULL; | |
560 | out: | |
561 | return meta; | |
562 | } | |
563 | ||
564 | void zram_init_device(struct zram *zram, struct zram_meta *meta) | |
565 | { | |
0231c403 MK |
566 | if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) { |
567 | pr_info( | |
568 | "There is little point creating a zram of greater than " | |
569 | "twice the size of memory since we expect a 2:1 compression " | |
570 | "ratio. Note that zram uses about 0.1%% of the size of " | |
571 | "the disk when not in use so a huge zram is " | |
572 | "wasteful.\n" | |
152bce6b | 573 | "\tMemory Size: %lu kB\n" |
0231c403 MK |
574 | "\tSize you selected: %llu kB\n" |
575 | "Continuing anyway ...\n", | |
576 | (totalram_pages << PAGE_SHIFT) >> 10, zram->disksize >> 10 | |
577 | ); | |
578 | } | |
306b0c95 | 579 | |
f1e3cfff NG |
580 | /* zram devices sort of resembles non-rotational disks */ |
581 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); | |
306b0c95 | 582 | |
8b3cc3ed | 583 | zram->meta = meta; |
f1e3cfff | 584 | zram->init_done = 1; |
306b0c95 NG |
585 | |
586 | pr_debug("Initialization done!\n"); | |
306b0c95 NG |
587 | } |
588 | ||
2ccbec05 NG |
589 | static void zram_slot_free_notify(struct block_device *bdev, |
590 | unsigned long index) | |
107c161b | 591 | { |
f1e3cfff | 592 | struct zram *zram; |
107c161b | 593 | |
f1e3cfff | 594 | zram = bdev->bd_disk->private_data; |
57ab0485 | 595 | down_write(&zram->lock); |
f1e3cfff | 596 | zram_free_page(zram, index); |
57ab0485 | 597 | up_write(&zram->lock); |
f1e3cfff | 598 | zram_stat64_inc(zram, &zram->stats.notify_free); |
107c161b NG |
599 | } |
600 | ||
f1e3cfff | 601 | static const struct block_device_operations zram_devops = { |
f1e3cfff | 602 | .swap_slot_free_notify = zram_slot_free_notify, |
107c161b | 603 | .owner = THIS_MODULE |
306b0c95 NG |
604 | }; |
605 | ||
f1e3cfff | 606 | static int create_device(struct zram *zram, int device_id) |
306b0c95 | 607 | { |
39a9b8ac | 608 | int ret = -ENOMEM; |
de1a21a0 | 609 | |
c5bde238 | 610 | init_rwsem(&zram->lock); |
0900beae | 611 | init_rwsem(&zram->init_lock); |
f1e3cfff | 612 | spin_lock_init(&zram->stat64_lock); |
306b0c95 | 613 | |
f1e3cfff NG |
614 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
615 | if (!zram->queue) { | |
306b0c95 NG |
616 | pr_err("Error allocating disk queue for device %d\n", |
617 | device_id); | |
de1a21a0 | 618 | goto out; |
306b0c95 NG |
619 | } |
620 | ||
f1e3cfff NG |
621 | blk_queue_make_request(zram->queue, zram_make_request); |
622 | zram->queue->queuedata = zram; | |
306b0c95 NG |
623 | |
624 | /* gendisk structure */ | |
f1e3cfff NG |
625 | zram->disk = alloc_disk(1); |
626 | if (!zram->disk) { | |
94b8435f | 627 | pr_warn("Error allocating disk structure for device %d\n", |
306b0c95 | 628 | device_id); |
39a9b8ac | 629 | goto out_free_queue; |
306b0c95 NG |
630 | } |
631 | ||
f1e3cfff NG |
632 | zram->disk->major = zram_major; |
633 | zram->disk->first_minor = device_id; | |
634 | zram->disk->fops = &zram_devops; | |
635 | zram->disk->queue = zram->queue; | |
636 | zram->disk->private_data = zram; | |
637 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); | |
306b0c95 | 638 | |
33863c21 | 639 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
f1e3cfff | 640 | set_capacity(zram->disk, 0); |
5d83d5a0 | 641 | |
a1dd52af NG |
642 | /* |
643 | * To ensure that we always get PAGE_SIZE aligned | |
644 | * and n*PAGE_SIZED sized I/O requests. | |
645 | */ | |
f1e3cfff | 646 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
7b19b8d4 RJ |
647 | blk_queue_logical_block_size(zram->disk->queue, |
648 | ZRAM_LOGICAL_BLOCK_SIZE); | |
f1e3cfff NG |
649 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
650 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); | |
5d83d5a0 | 651 | |
f1e3cfff | 652 | add_disk(zram->disk); |
306b0c95 | 653 | |
33863c21 NG |
654 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
655 | &zram_disk_attr_group); | |
656 | if (ret < 0) { | |
94b8435f | 657 | pr_warn("Error creating sysfs group"); |
39a9b8ac | 658 | goto out_free_disk; |
33863c21 | 659 | } |
33863c21 | 660 | |
f1e3cfff | 661 | zram->init_done = 0; |
39a9b8ac | 662 | return 0; |
de1a21a0 | 663 | |
39a9b8ac JL |
664 | out_free_disk: |
665 | del_gendisk(zram->disk); | |
666 | put_disk(zram->disk); | |
667 | out_free_queue: | |
668 | blk_cleanup_queue(zram->queue); | |
de1a21a0 NG |
669 | out: |
670 | return ret; | |
306b0c95 NG |
671 | } |
672 | ||
f1e3cfff | 673 | static void destroy_device(struct zram *zram) |
306b0c95 | 674 | { |
33863c21 NG |
675 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
676 | &zram_disk_attr_group); | |
33863c21 | 677 | |
f1e3cfff NG |
678 | if (zram->disk) { |
679 | del_gendisk(zram->disk); | |
680 | put_disk(zram->disk); | |
306b0c95 NG |
681 | } |
682 | ||
f1e3cfff NG |
683 | if (zram->queue) |
684 | blk_cleanup_queue(zram->queue); | |
306b0c95 NG |
685 | } |
686 | ||
f1e3cfff | 687 | static int __init zram_init(void) |
306b0c95 | 688 | { |
de1a21a0 | 689 | int ret, dev_id; |
306b0c95 | 690 | |
5fa5a901 | 691 | if (num_devices > max_num_devices) { |
94b8435f | 692 | pr_warn("Invalid value for num_devices: %u\n", |
5fa5a901 | 693 | num_devices); |
de1a21a0 NG |
694 | ret = -EINVAL; |
695 | goto out; | |
306b0c95 NG |
696 | } |
697 | ||
f1e3cfff NG |
698 | zram_major = register_blkdev(0, "zram"); |
699 | if (zram_major <= 0) { | |
94b8435f | 700 | pr_warn("Unable to get major number\n"); |
de1a21a0 NG |
701 | ret = -EBUSY; |
702 | goto out; | |
306b0c95 NG |
703 | } |
704 | ||
306b0c95 | 705 | /* Allocate the device array and initialize each one */ |
5fa5a901 | 706 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
43801f6e | 707 | if (!zram_devices) { |
de1a21a0 NG |
708 | ret = -ENOMEM; |
709 | goto unregister; | |
710 | } | |
306b0c95 | 711 | |
5fa5a901 | 712 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
43801f6e | 713 | ret = create_device(&zram_devices[dev_id], dev_id); |
de1a21a0 | 714 | if (ret) |
3bf040c7 | 715 | goto free_devices; |
de1a21a0 NG |
716 | } |
717 | ||
ca3d70bd DB |
718 | pr_info("Created %u device(s) ...\n", num_devices); |
719 | ||
306b0c95 | 720 | return 0; |
de1a21a0 | 721 | |
3bf040c7 | 722 | free_devices: |
de1a21a0 | 723 | while (dev_id) |
43801f6e NW |
724 | destroy_device(&zram_devices[--dev_id]); |
725 | kfree(zram_devices); | |
de1a21a0 | 726 | unregister: |
f1e3cfff | 727 | unregister_blkdev(zram_major, "zram"); |
de1a21a0 | 728 | out: |
306b0c95 NG |
729 | return ret; |
730 | } | |
731 | ||
f1e3cfff | 732 | static void __exit zram_exit(void) |
306b0c95 NG |
733 | { |
734 | int i; | |
f1e3cfff | 735 | struct zram *zram; |
306b0c95 | 736 | |
5fa5a901 | 737 | for (i = 0; i < num_devices; i++) { |
43801f6e | 738 | zram = &zram_devices[i]; |
306b0c95 | 739 | |
6030ea9b | 740 | get_disk(zram->disk); |
f1e3cfff | 741 | destroy_device(zram); |
0231c403 | 742 | zram_reset_device(zram); |
6030ea9b | 743 | put_disk(zram->disk); |
306b0c95 NG |
744 | } |
745 | ||
f1e3cfff | 746 | unregister_blkdev(zram_major, "zram"); |
306b0c95 | 747 | |
43801f6e | 748 | kfree(zram_devices); |
306b0c95 NG |
749 | pr_debug("Cleanup done!\n"); |
750 | } | |
751 | ||
5fa5a901 NG |
752 | module_param(num_devices, uint, 0); |
753 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); | |
306b0c95 | 754 | |
f1e3cfff NG |
755 | module_init(zram_init); |
756 | module_exit(zram_exit); | |
306b0c95 NG |
757 | |
758 | MODULE_LICENSE("Dual BSD/GPL"); | |
759 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); | |
f1e3cfff | 760 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |