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