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