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; |
306b0c95 | 139 | |
fd1a30de | 140 | if (unlikely(!handle)) { |
2e882281 NG |
141 | /* |
142 | * No memory is allocated for zero filled pages. | |
143 | * Simply clear zero page flag. | |
144 | */ | |
f1e3cfff NG |
145 | if (zram_test_flag(zram, index, ZRAM_ZERO)) { |
146 | zram_clear_flag(zram, index, ZRAM_ZERO); | |
147 | zram_stat_dec(&zram->stats.pages_zero); | |
306b0c95 NG |
148 | } |
149 | return; | |
150 | } | |
151 | ||
f1e3cfff | 152 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) { |
c2344348 | 153 | __free_page((struct page *)handle); |
f1e3cfff NG |
154 | zram_clear_flag(zram, index, ZRAM_UNCOMPRESSED); |
155 | zram_stat_dec(&zram->stats.pages_expand); | |
306b0c95 NG |
156 | goto out; |
157 | } | |
158 | ||
fd1a30de | 159 | zs_free(zram->mem_pool, handle); |
306b0c95 | 160 | |
fd1a30de | 161 | if (zram->table[index].size <= PAGE_SIZE / 2) |
f1e3cfff | 162 | zram_stat_dec(&zram->stats.good_compress); |
306b0c95 NG |
163 | |
164 | out: | |
fd1a30de NG |
165 | zram_stat64_sub(zram, &zram->stats.compr_size, |
166 | zram->table[index].size); | |
f1e3cfff | 167 | zram_stat_dec(&zram->stats.pages_stored); |
306b0c95 | 168 | |
c2344348 | 169 | zram->table[index].handle = 0; |
fd1a30de | 170 | zram->table[index].size = 0; |
306b0c95 NG |
171 | } |
172 | ||
924bd88d | 173 | static void handle_zero_page(struct bio_vec *bvec) |
306b0c95 | 174 | { |
924bd88d | 175 | struct page *page = bvec->bv_page; |
306b0c95 | 176 | void *user_mem; |
306b0c95 | 177 | |
ba82fe2e | 178 | user_mem = kmap_atomic(page); |
924bd88d | 179 | memset(user_mem + bvec->bv_offset, 0, bvec->bv_len); |
ba82fe2e | 180 | kunmap_atomic(user_mem); |
306b0c95 | 181 | |
30fb8a71 | 182 | flush_dcache_page(page); |
306b0c95 NG |
183 | } |
184 | ||
924bd88d JM |
185 | static void handle_uncompressed_page(struct zram *zram, struct bio_vec *bvec, |
186 | u32 index, int offset) | |
306b0c95 | 187 | { |
924bd88d | 188 | struct page *page = bvec->bv_page; |
306b0c95 NG |
189 | unsigned char *user_mem, *cmem; |
190 | ||
ba82fe2e | 191 | user_mem = kmap_atomic(page); |
c2344348 | 192 | cmem = kmap_atomic((struct page *)zram->table[index].handle); |
306b0c95 | 193 | |
924bd88d | 194 | memcpy(user_mem + bvec->bv_offset, cmem + offset, bvec->bv_len); |
ba82fe2e CW |
195 | kunmap_atomic(cmem); |
196 | kunmap_atomic(user_mem); | |
306b0c95 | 197 | |
30fb8a71 | 198 | flush_dcache_page(page); |
306b0c95 NG |
199 | } |
200 | ||
924bd88d JM |
201 | static inline int is_partial_io(struct bio_vec *bvec) |
202 | { | |
203 | return bvec->bv_len != PAGE_SIZE; | |
204 | } | |
205 | ||
8c921b2b | 206 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
924bd88d | 207 | u32 index, int offset, struct bio *bio) |
306b0c95 | 208 | { |
8c921b2b JM |
209 | int ret; |
210 | size_t clen; | |
211 | struct page *page; | |
212 | struct zobj_header *zheader; | |
924bd88d | 213 | unsigned char *user_mem, *cmem, *uncmem = NULL; |
a1dd52af | 214 | |
8c921b2b | 215 | page = bvec->bv_page; |
306b0c95 | 216 | |
8c921b2b | 217 | if (zram_test_flag(zram, index, ZRAM_ZERO)) { |
924bd88d | 218 | handle_zero_page(bvec); |
8c921b2b JM |
219 | return 0; |
220 | } | |
306b0c95 | 221 | |
8c921b2b | 222 | /* Requested page is not present in compressed area */ |
fd1a30de | 223 | if (unlikely(!zram->table[index].handle)) { |
8c921b2b JM |
224 | pr_debug("Read before write: sector=%lu, size=%u", |
225 | (ulong)(bio->bi_sector), bio->bi_size); | |
924bd88d | 226 | handle_zero_page(bvec); |
8c921b2b JM |
227 | return 0; |
228 | } | |
306b0c95 | 229 | |
8c921b2b JM |
230 | /* Page is stored uncompressed since it's incompressible */ |
231 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) { | |
924bd88d | 232 | handle_uncompressed_page(zram, bvec, index, offset); |
8c921b2b JM |
233 | return 0; |
234 | } | |
306b0c95 | 235 | |
924bd88d JM |
236 | if (is_partial_io(bvec)) { |
237 | /* Use a temporary buffer to decompress the page */ | |
238 | uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
239 | if (!uncmem) { | |
240 | pr_info("Error allocating temp memory!\n"); | |
241 | return -ENOMEM; | |
242 | } | |
243 | } | |
244 | ||
ba82fe2e | 245 | user_mem = kmap_atomic(page); |
924bd88d JM |
246 | if (!is_partial_io(bvec)) |
247 | uncmem = user_mem; | |
8c921b2b | 248 | clen = PAGE_SIZE; |
306b0c95 | 249 | |
fd1a30de | 250 | cmem = zs_map_object(zram->mem_pool, zram->table[index].handle); |
306b0c95 | 251 | |
8c921b2b | 252 | ret = lzo1x_decompress_safe(cmem + sizeof(*zheader), |
fd1a30de | 253 | zram->table[index].size, |
924bd88d JM |
254 | uncmem, &clen); |
255 | ||
256 | if (is_partial_io(bvec)) { | |
257 | memcpy(user_mem + bvec->bv_offset, uncmem + offset, | |
258 | bvec->bv_len); | |
259 | kfree(uncmem); | |
260 | } | |
306b0c95 | 261 | |
fd1a30de | 262 | zs_unmap_object(zram->mem_pool, zram->table[index].handle); |
ba82fe2e | 263 | kunmap_atomic(user_mem); |
a1dd52af | 264 | |
8c921b2b JM |
265 | /* Should NEVER happen. Return bio error if it does. */ |
266 | if (unlikely(ret != LZO_E_OK)) { | |
267 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); | |
268 | zram_stat64_inc(zram, &zram->stats.failed_reads); | |
269 | return ret; | |
a1dd52af | 270 | } |
306b0c95 | 271 | |
8c921b2b | 272 | flush_dcache_page(page); |
306b0c95 | 273 | |
8c921b2b | 274 | return 0; |
306b0c95 NG |
275 | } |
276 | ||
924bd88d JM |
277 | static int zram_read_before_write(struct zram *zram, char *mem, u32 index) |
278 | { | |
279 | int ret; | |
280 | size_t clen = PAGE_SIZE; | |
281 | struct zobj_header *zheader; | |
282 | unsigned char *cmem; | |
374a6919 | 283 | unsigned long handle = zram->table[index].handle; |
924bd88d | 284 | |
374a6919 | 285 | if (zram_test_flag(zram, index, ZRAM_ZERO) || !handle) { |
924bd88d JM |
286 | memset(mem, 0, PAGE_SIZE); |
287 | return 0; | |
288 | } | |
289 | ||
924bd88d JM |
290 | /* Page is stored uncompressed since it's incompressible */ |
291 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) { | |
374a6919 MK |
292 | char *src = kmap_atomic((struct page *)handle); |
293 | memcpy(mem, src, PAGE_SIZE); | |
294 | kunmap_atomic(src); | |
924bd88d JM |
295 | return 0; |
296 | } | |
297 | ||
374a6919 MK |
298 | cmem = zs_map_object(zram->mem_pool, handle); |
299 | ||
924bd88d | 300 | ret = lzo1x_decompress_safe(cmem + sizeof(*zheader), |
fd1a30de | 301 | zram->table[index].size, |
924bd88d | 302 | mem, &clen); |
374a6919 | 303 | zs_unmap_object(zram->mem_pool, handle); |
924bd88d JM |
304 | |
305 | /* Should NEVER happen. Return bio error if it does. */ | |
306 | if (unlikely(ret != LZO_E_OK)) { | |
307 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); | |
308 | zram_stat64_inc(zram, &zram->stats.failed_reads); | |
309 | return ret; | |
310 | } | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
315 | static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, | |
316 | int offset) | |
306b0c95 | 317 | { |
8c921b2b | 318 | int ret; |
924bd88d | 319 | u32 store_offset; |
8c921b2b | 320 | size_t clen; |
c2344348 | 321 | unsigned long handle; |
8c921b2b JM |
322 | struct zobj_header *zheader; |
323 | struct page *page, *page_store; | |
924bd88d | 324 | unsigned char *user_mem, *cmem, *src, *uncmem = NULL; |
306b0c95 | 325 | |
8c921b2b JM |
326 | page = bvec->bv_page; |
327 | src = zram->compress_buffer; | |
306b0c95 | 328 | |
924bd88d JM |
329 | if (is_partial_io(bvec)) { |
330 | /* | |
331 | * This is a partial IO. We need to read the full page | |
332 | * before to write the changes. | |
333 | */ | |
334 | uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
335 | if (!uncmem) { | |
336 | pr_info("Error allocating temp memory!\n"); | |
337 | ret = -ENOMEM; | |
338 | goto out; | |
339 | } | |
340 | ret = zram_read_before_write(zram, uncmem, index); | |
341 | if (ret) { | |
342 | kfree(uncmem); | |
343 | goto out; | |
344 | } | |
345 | } | |
346 | ||
8c921b2b JM |
347 | /* |
348 | * System overwrites unused sectors. Free memory associated | |
349 | * with this sector now. | |
350 | */ | |
fd1a30de | 351 | if (zram->table[index].handle || |
8c921b2b JM |
352 | zram_test_flag(zram, index, ZRAM_ZERO)) |
353 | zram_free_page(zram, index); | |
306b0c95 | 354 | |
ba82fe2e | 355 | user_mem = kmap_atomic(page); |
924bd88d JM |
356 | |
357 | if (is_partial_io(bvec)) | |
358 | memcpy(uncmem + offset, user_mem + bvec->bv_offset, | |
359 | bvec->bv_len); | |
360 | else | |
361 | uncmem = user_mem; | |
362 | ||
363 | if (page_zero_filled(uncmem)) { | |
ba82fe2e | 364 | kunmap_atomic(user_mem); |
924bd88d JM |
365 | if (is_partial_io(bvec)) |
366 | kfree(uncmem); | |
8c921b2b JM |
367 | zram_stat_inc(&zram->stats.pages_zero); |
368 | zram_set_flag(zram, index, ZRAM_ZERO); | |
924bd88d JM |
369 | ret = 0; |
370 | goto out; | |
8c921b2b | 371 | } |
306b0c95 | 372 | |
924bd88d | 373 | ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen, |
8c921b2b | 374 | zram->compress_workmem); |
306b0c95 | 375 | |
ba82fe2e | 376 | kunmap_atomic(user_mem); |
924bd88d JM |
377 | if (is_partial_io(bvec)) |
378 | kfree(uncmem); | |
306b0c95 | 379 | |
8c921b2b | 380 | if (unlikely(ret != LZO_E_OK)) { |
8c921b2b | 381 | pr_err("Compression failed! err=%d\n", ret); |
924bd88d | 382 | goto out; |
8c921b2b | 383 | } |
306b0c95 | 384 | |
8c921b2b JM |
385 | /* |
386 | * Page is incompressible. Store it as-is (uncompressed) | |
387 | * since we do not want to return too many disk write | |
388 | * errors which has side effect of hanging the system. | |
389 | */ | |
390 | if (unlikely(clen > max_zpage_size)) { | |
391 | clen = PAGE_SIZE; | |
392 | page_store = alloc_page(GFP_NOIO | __GFP_HIGHMEM); | |
393 | if (unlikely(!page_store)) { | |
8c921b2b JM |
394 | pr_info("Error allocating memory for " |
395 | "incompressible page: %u\n", index); | |
924bd88d JM |
396 | ret = -ENOMEM; |
397 | goto out; | |
398 | } | |
a1dd52af | 399 | |
924bd88d | 400 | store_offset = 0; |
8c921b2b JM |
401 | zram_set_flag(zram, index, ZRAM_UNCOMPRESSED); |
402 | zram_stat_inc(&zram->stats.pages_expand); | |
c2344348 | 403 | handle = (unsigned long)page_store; |
ba82fe2e | 404 | src = kmap_atomic(page); |
9f393834 | 405 | cmem = kmap_atomic(page_store); |
8c921b2b JM |
406 | goto memstore; |
407 | } | |
306b0c95 | 408 | |
fd1a30de NG |
409 | handle = zs_malloc(zram->mem_pool, clen + sizeof(*zheader)); |
410 | if (!handle) { | |
8c921b2b JM |
411 | pr_info("Error allocating memory for compressed " |
412 | "page: %u, size=%zu\n", index, clen); | |
924bd88d JM |
413 | ret = -ENOMEM; |
414 | goto out; | |
8c921b2b | 415 | } |
fd1a30de | 416 | cmem = zs_map_object(zram->mem_pool, handle); |
306b0c95 NG |
417 | |
418 | memstore: | |
306b0c95 | 419 | #if 0 |
8c921b2b JM |
420 | /* Back-reference needed for memory defragmentation */ |
421 | if (!zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)) { | |
422 | zheader = (struct zobj_header *)cmem; | |
423 | zheader->table_idx = index; | |
424 | cmem += sizeof(*zheader); | |
425 | } | |
306b0c95 NG |
426 | #endif |
427 | ||
8c921b2b | 428 | memcpy(cmem, src, clen); |
306b0c95 | 429 | |
fd1a30de | 430 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) { |
9f393834 | 431 | kunmap_atomic(cmem); |
ba82fe2e | 432 | kunmap_atomic(src); |
fd1a30de NG |
433 | } else { |
434 | zs_unmap_object(zram->mem_pool, handle); | |
435 | } | |
436 | ||
437 | zram->table[index].handle = handle; | |
438 | zram->table[index].size = clen; | |
306b0c95 | 439 | |
8c921b2b JM |
440 | /* Update stats */ |
441 | zram_stat64_add(zram, &zram->stats.compr_size, clen); | |
442 | zram_stat_inc(&zram->stats.pages_stored); | |
443 | if (clen <= PAGE_SIZE / 2) | |
444 | zram_stat_inc(&zram->stats.good_compress); | |
306b0c95 | 445 | |
8c921b2b | 446 | return 0; |
924bd88d JM |
447 | |
448 | out: | |
449 | if (ret) | |
450 | zram_stat64_inc(zram, &zram->stats.failed_writes); | |
451 | return ret; | |
8c921b2b JM |
452 | } |
453 | ||
454 | static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, | |
924bd88d | 455 | int offset, struct bio *bio, int rw) |
8c921b2b | 456 | { |
c5bde238 | 457 | int ret; |
8c921b2b | 458 | |
c5bde238 JM |
459 | if (rw == READ) { |
460 | down_read(&zram->lock); | |
461 | ret = zram_bvec_read(zram, bvec, index, offset, bio); | |
462 | up_read(&zram->lock); | |
463 | } else { | |
464 | down_write(&zram->lock); | |
465 | ret = zram_bvec_write(zram, bvec, index, offset); | |
466 | up_write(&zram->lock); | |
467 | } | |
468 | ||
469 | return ret; | |
924bd88d JM |
470 | } |
471 | ||
472 | static void update_position(u32 *index, int *offset, struct bio_vec *bvec) | |
473 | { | |
474 | if (*offset + bvec->bv_len >= PAGE_SIZE) | |
475 | (*index)++; | |
476 | *offset = (*offset + bvec->bv_len) % PAGE_SIZE; | |
8c921b2b JM |
477 | } |
478 | ||
479 | static void __zram_make_request(struct zram *zram, struct bio *bio, int rw) | |
480 | { | |
924bd88d | 481 | int i, offset; |
8c921b2b JM |
482 | u32 index; |
483 | struct bio_vec *bvec; | |
484 | ||
485 | switch (rw) { | |
486 | case READ: | |
487 | zram_stat64_inc(zram, &zram->stats.num_reads); | |
488 | break; | |
489 | case WRITE: | |
490 | zram_stat64_inc(zram, &zram->stats.num_writes); | |
491 | break; | |
492 | } | |
493 | ||
494 | index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT; | |
924bd88d | 495 | offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; |
8c921b2b JM |
496 | |
497 | bio_for_each_segment(bvec, bio, i) { | |
924bd88d JM |
498 | int max_transfer_size = PAGE_SIZE - offset; |
499 | ||
500 | if (bvec->bv_len > max_transfer_size) { | |
501 | /* | |
502 | * zram_bvec_rw() can only make operation on a single | |
503 | * zram page. Split the bio vector. | |
504 | */ | |
505 | struct bio_vec bv; | |
506 | ||
507 | bv.bv_page = bvec->bv_page; | |
508 | bv.bv_len = max_transfer_size; | |
509 | bv.bv_offset = bvec->bv_offset; | |
510 | ||
511 | if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0) | |
512 | goto out; | |
513 | ||
514 | bv.bv_len = bvec->bv_len - max_transfer_size; | |
515 | bv.bv_offset += max_transfer_size; | |
516 | if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0) | |
517 | goto out; | |
518 | } else | |
519 | if (zram_bvec_rw(zram, bvec, index, offset, bio, rw) | |
520 | < 0) | |
521 | goto out; | |
522 | ||
523 | update_position(&index, &offset, bvec); | |
a1dd52af | 524 | } |
306b0c95 NG |
525 | |
526 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
527 | bio_endio(bio, 0); | |
7d7854b4 | 528 | return; |
306b0c95 NG |
529 | |
530 | out: | |
306b0c95 | 531 | bio_io_error(bio); |
306b0c95 NG |
532 | } |
533 | ||
306b0c95 | 534 | /* |
924bd88d | 535 | * Check if request is within bounds and aligned on zram logical blocks. |
306b0c95 | 536 | */ |
f1e3cfff | 537 | static inline int valid_io_request(struct zram *zram, struct bio *bio) |
306b0c95 NG |
538 | { |
539 | if (unlikely( | |
f1e3cfff | 540 | (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) || |
924bd88d JM |
541 | (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) || |
542 | (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) { | |
306b0c95 NG |
543 | |
544 | return 0; | |
545 | } | |
546 | ||
a1dd52af | 547 | /* I/O request is valid */ |
306b0c95 NG |
548 | return 1; |
549 | } | |
550 | ||
551 | /* | |
f1e3cfff | 552 | * Handler function for all zram I/O requests. |
306b0c95 | 553 | */ |
5a7bbad2 | 554 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
306b0c95 | 555 | { |
f1e3cfff | 556 | struct zram *zram = queue->queuedata; |
306b0c95 | 557 | |
0900beae JM |
558 | if (unlikely(!zram->init_done) && zram_init_device(zram)) |
559 | goto error; | |
560 | ||
561 | down_read(&zram->init_lock); | |
562 | if (unlikely(!zram->init_done)) | |
563 | goto error_unlock; | |
564 | ||
f1e3cfff NG |
565 | if (!valid_io_request(zram, bio)) { |
566 | zram_stat64_inc(zram, &zram->stats.invalid_io); | |
0900beae | 567 | goto error_unlock; |
6642a67c JM |
568 | } |
569 | ||
8c921b2b | 570 | __zram_make_request(zram, bio, bio_data_dir(bio)); |
0900beae | 571 | up_read(&zram->init_lock); |
306b0c95 | 572 | |
b4fdcb02 | 573 | return; |
0900beae JM |
574 | |
575 | error_unlock: | |
576 | up_read(&zram->init_lock); | |
577 | error: | |
578 | bio_io_error(bio); | |
306b0c95 NG |
579 | } |
580 | ||
0900beae | 581 | void __zram_reset_device(struct zram *zram) |
306b0c95 | 582 | { |
97a06382 | 583 | size_t index; |
306b0c95 | 584 | |
f1e3cfff | 585 | zram->init_done = 0; |
7eef7533 | 586 | |
306b0c95 | 587 | /* Free various per-device buffers */ |
f1e3cfff NG |
588 | kfree(zram->compress_workmem); |
589 | free_pages((unsigned long)zram->compress_buffer, 1); | |
306b0c95 | 590 | |
f1e3cfff NG |
591 | zram->compress_workmem = NULL; |
592 | zram->compress_buffer = NULL; | |
306b0c95 | 593 | |
f1e3cfff NG |
594 | /* Free all pages that are still in this zram device */ |
595 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { | |
c2344348 | 596 | unsigned long handle = zram->table[index].handle; |
fd1a30de | 597 | if (!handle) |
306b0c95 NG |
598 | continue; |
599 | ||
f1e3cfff | 600 | if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) |
c2344348 | 601 | __free_page((struct page *)handle); |
306b0c95 | 602 | else |
fd1a30de | 603 | zs_free(zram->mem_pool, handle); |
306b0c95 NG |
604 | } |
605 | ||
f1e3cfff NG |
606 | vfree(zram->table); |
607 | zram->table = NULL; | |
306b0c95 | 608 | |
fd1a30de | 609 | zs_destroy_pool(zram->mem_pool); |
f1e3cfff | 610 | zram->mem_pool = NULL; |
306b0c95 | 611 | |
306b0c95 | 612 | /* Reset stats */ |
f1e3cfff | 613 | memset(&zram->stats, 0, sizeof(zram->stats)); |
306b0c95 | 614 | |
f1e3cfff | 615 | zram->disksize = 0; |
0900beae JM |
616 | } |
617 | ||
618 | void zram_reset_device(struct zram *zram) | |
619 | { | |
620 | down_write(&zram->init_lock); | |
621 | __zram_reset_device(zram); | |
622 | up_write(&zram->init_lock); | |
306b0c95 NG |
623 | } |
624 | ||
33863c21 | 625 | int zram_init_device(struct zram *zram) |
306b0c95 NG |
626 | { |
627 | int ret; | |
628 | size_t num_pages; | |
306b0c95 | 629 | |
0900beae | 630 | down_write(&zram->init_lock); |
484875ad | 631 | |
f1e3cfff | 632 | if (zram->init_done) { |
0900beae | 633 | up_write(&zram->init_lock); |
484875ad | 634 | return 0; |
306b0c95 NG |
635 | } |
636 | ||
f1e3cfff | 637 | zram_set_disksize(zram, totalram_pages << PAGE_SHIFT); |
306b0c95 | 638 | |
f1e3cfff NG |
639 | zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
640 | if (!zram->compress_workmem) { | |
306b0c95 NG |
641 | pr_err("Error allocating compressor working memory!\n"); |
642 | ret = -ENOMEM; | |
5a18c531 | 643 | goto fail_no_table; |
306b0c95 NG |
644 | } |
645 | ||
fb927284 JM |
646 | zram->compress_buffer = |
647 | (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); | |
f1e3cfff | 648 | if (!zram->compress_buffer) { |
306b0c95 NG |
649 | pr_err("Error allocating compressor buffer space\n"); |
650 | ret = -ENOMEM; | |
5a18c531 | 651 | goto fail_no_table; |
306b0c95 NG |
652 | } |
653 | ||
f1e3cfff | 654 | num_pages = zram->disksize >> PAGE_SHIFT; |
5b84cc78 | 655 | zram->table = vzalloc(num_pages * sizeof(*zram->table)); |
f1e3cfff NG |
656 | if (!zram->table) { |
657 | pr_err("Error allocating zram address table\n"); | |
306b0c95 | 658 | ret = -ENOMEM; |
5a18c531 | 659 | goto fail_no_table; |
306b0c95 | 660 | } |
306b0c95 | 661 | |
f1e3cfff | 662 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
306b0c95 | 663 | |
f1e3cfff NG |
664 | /* zram devices sort of resembles non-rotational disks */ |
665 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); | |
306b0c95 | 666 | |
fd1a30de | 667 | zram->mem_pool = zs_create_pool("zram", GFP_NOIO | __GFP_HIGHMEM); |
f1e3cfff | 668 | if (!zram->mem_pool) { |
306b0c95 NG |
669 | pr_err("Error creating memory pool\n"); |
670 | ret = -ENOMEM; | |
671 | goto fail; | |
672 | } | |
673 | ||
f1e3cfff | 674 | zram->init_done = 1; |
0900beae | 675 | up_write(&zram->init_lock); |
306b0c95 NG |
676 | |
677 | pr_debug("Initialization done!\n"); | |
678 | return 0; | |
679 | ||
5a18c531 JM |
680 | fail_no_table: |
681 | /* To prevent accessing table entries during cleanup */ | |
682 | zram->disksize = 0; | |
306b0c95 | 683 | fail: |
0900beae JM |
684 | __zram_reset_device(zram); |
685 | up_write(&zram->init_lock); | |
306b0c95 NG |
686 | pr_err("Initialization failed: err=%d\n", ret); |
687 | return ret; | |
688 | } | |
689 | ||
2ccbec05 NG |
690 | static void zram_slot_free_notify(struct block_device *bdev, |
691 | unsigned long index) | |
107c161b | 692 | { |
f1e3cfff | 693 | struct zram *zram; |
107c161b | 694 | |
f1e3cfff NG |
695 | zram = bdev->bd_disk->private_data; |
696 | zram_free_page(zram, index); | |
697 | zram_stat64_inc(zram, &zram->stats.notify_free); | |
107c161b NG |
698 | } |
699 | ||
f1e3cfff | 700 | static const struct block_device_operations zram_devops = { |
f1e3cfff | 701 | .swap_slot_free_notify = zram_slot_free_notify, |
107c161b | 702 | .owner = THIS_MODULE |
306b0c95 NG |
703 | }; |
704 | ||
f1e3cfff | 705 | static int create_device(struct zram *zram, int device_id) |
306b0c95 | 706 | { |
de1a21a0 NG |
707 | int ret = 0; |
708 | ||
c5bde238 | 709 | init_rwsem(&zram->lock); |
0900beae | 710 | init_rwsem(&zram->init_lock); |
f1e3cfff | 711 | spin_lock_init(&zram->stat64_lock); |
306b0c95 | 712 | |
f1e3cfff NG |
713 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
714 | if (!zram->queue) { | |
306b0c95 NG |
715 | pr_err("Error allocating disk queue for device %d\n", |
716 | device_id); | |
de1a21a0 NG |
717 | ret = -ENOMEM; |
718 | goto out; | |
306b0c95 NG |
719 | } |
720 | ||
f1e3cfff NG |
721 | blk_queue_make_request(zram->queue, zram_make_request); |
722 | zram->queue->queuedata = zram; | |
306b0c95 NG |
723 | |
724 | /* gendisk structure */ | |
f1e3cfff NG |
725 | zram->disk = alloc_disk(1); |
726 | if (!zram->disk) { | |
727 | blk_cleanup_queue(zram->queue); | |
306b0c95 NG |
728 | pr_warning("Error allocating disk structure for device %d\n", |
729 | device_id); | |
de1a21a0 NG |
730 | ret = -ENOMEM; |
731 | goto out; | |
306b0c95 NG |
732 | } |
733 | ||
f1e3cfff NG |
734 | zram->disk->major = zram_major; |
735 | zram->disk->first_minor = device_id; | |
736 | zram->disk->fops = &zram_devops; | |
737 | zram->disk->queue = zram->queue; | |
738 | zram->disk->private_data = zram; | |
739 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); | |
306b0c95 | 740 | |
33863c21 | 741 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
f1e3cfff | 742 | set_capacity(zram->disk, 0); |
5d83d5a0 | 743 | |
a1dd52af NG |
744 | /* |
745 | * To ensure that we always get PAGE_SIZE aligned | |
746 | * and n*PAGE_SIZED sized I/O requests. | |
747 | */ | |
f1e3cfff | 748 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
7b19b8d4 RJ |
749 | blk_queue_logical_block_size(zram->disk->queue, |
750 | ZRAM_LOGICAL_BLOCK_SIZE); | |
f1e3cfff NG |
751 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
752 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); | |
5d83d5a0 | 753 | |
f1e3cfff | 754 | add_disk(zram->disk); |
306b0c95 | 755 | |
33863c21 NG |
756 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
757 | &zram_disk_attr_group); | |
758 | if (ret < 0) { | |
759 | pr_warning("Error creating sysfs group"); | |
760 | goto out; | |
761 | } | |
33863c21 | 762 | |
f1e3cfff | 763 | zram->init_done = 0; |
de1a21a0 NG |
764 | |
765 | out: | |
766 | return ret; | |
306b0c95 NG |
767 | } |
768 | ||
f1e3cfff | 769 | static void destroy_device(struct zram *zram) |
306b0c95 | 770 | { |
33863c21 NG |
771 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
772 | &zram_disk_attr_group); | |
33863c21 | 773 | |
f1e3cfff NG |
774 | if (zram->disk) { |
775 | del_gendisk(zram->disk); | |
776 | put_disk(zram->disk); | |
306b0c95 NG |
777 | } |
778 | ||
f1e3cfff NG |
779 | if (zram->queue) |
780 | blk_cleanup_queue(zram->queue); | |
306b0c95 NG |
781 | } |
782 | ||
5fa5a901 NG |
783 | unsigned int zram_get_num_devices(void) |
784 | { | |
785 | return num_devices; | |
786 | } | |
787 | ||
f1e3cfff | 788 | static int __init zram_init(void) |
306b0c95 | 789 | { |
de1a21a0 | 790 | int ret, dev_id; |
306b0c95 | 791 | |
5fa5a901 | 792 | if (num_devices > max_num_devices) { |
306b0c95 | 793 | pr_warning("Invalid value for num_devices: %u\n", |
5fa5a901 | 794 | num_devices); |
de1a21a0 NG |
795 | ret = -EINVAL; |
796 | goto out; | |
306b0c95 NG |
797 | } |
798 | ||
f1e3cfff NG |
799 | zram_major = register_blkdev(0, "zram"); |
800 | if (zram_major <= 0) { | |
306b0c95 | 801 | pr_warning("Unable to get major number\n"); |
de1a21a0 NG |
802 | ret = -EBUSY; |
803 | goto out; | |
306b0c95 NG |
804 | } |
805 | ||
5fa5a901 | 806 | if (!num_devices) { |
306b0c95 | 807 | pr_info("num_devices not specified. Using default: 1\n"); |
5fa5a901 | 808 | num_devices = 1; |
306b0c95 NG |
809 | } |
810 | ||
811 | /* Allocate the device array and initialize each one */ | |
5fa5a901 NG |
812 | pr_info("Creating %u devices ...\n", num_devices); |
813 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); | |
43801f6e | 814 | if (!zram_devices) { |
de1a21a0 NG |
815 | ret = -ENOMEM; |
816 | goto unregister; | |
817 | } | |
306b0c95 | 818 | |
5fa5a901 | 819 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
43801f6e | 820 | ret = create_device(&zram_devices[dev_id], dev_id); |
de1a21a0 | 821 | if (ret) |
3bf040c7 | 822 | goto free_devices; |
de1a21a0 NG |
823 | } |
824 | ||
306b0c95 | 825 | return 0; |
de1a21a0 | 826 | |
3bf040c7 | 827 | free_devices: |
de1a21a0 | 828 | while (dev_id) |
43801f6e NW |
829 | destroy_device(&zram_devices[--dev_id]); |
830 | kfree(zram_devices); | |
de1a21a0 | 831 | unregister: |
f1e3cfff | 832 | unregister_blkdev(zram_major, "zram"); |
de1a21a0 | 833 | out: |
306b0c95 NG |
834 | return ret; |
835 | } | |
836 | ||
f1e3cfff | 837 | static void __exit zram_exit(void) |
306b0c95 NG |
838 | { |
839 | int i; | |
f1e3cfff | 840 | struct zram *zram; |
306b0c95 | 841 | |
5fa5a901 | 842 | for (i = 0; i < num_devices; i++) { |
43801f6e | 843 | zram = &zram_devices[i]; |
306b0c95 | 844 | |
f1e3cfff NG |
845 | destroy_device(zram); |
846 | if (zram->init_done) | |
33863c21 | 847 | zram_reset_device(zram); |
306b0c95 NG |
848 | } |
849 | ||
f1e3cfff | 850 | unregister_blkdev(zram_major, "zram"); |
306b0c95 | 851 | |
43801f6e | 852 | kfree(zram_devices); |
306b0c95 NG |
853 | pr_debug("Cleanup done!\n"); |
854 | } | |
855 | ||
5fa5a901 NG |
856 | module_param(num_devices, uint, 0); |
857 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); | |
306b0c95 | 858 | |
f1e3cfff NG |
859 | module_init(zram_init); |
860 | module_exit(zram_exit); | |
306b0c95 NG |
861 | |
862 | MODULE_LICENSE("Dual BSD/GPL"); | |
863 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); | |
f1e3cfff | 864 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |