Commit | Line | Data |
---|---|---|
306b0c95 | 1 | /* |
f1e3cfff | 2 | * Compressed RAM block device |
306b0c95 | 3 | * |
1130ebba | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
7bfb3de8 | 5 | * 2012, 2013 Minchan Kim |
306b0c95 NG |
6 | * |
7 | * This code is released using a dual license strategy: BSD/GPL | |
8 | * You can choose the licence that better fits your requirements. | |
9 | * | |
10 | * Released under the terms of 3-clause BSD License | |
11 | * Released under the terms of GNU General Public License Version 2.0 | |
12 | * | |
306b0c95 NG |
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 | |
be2d1d56 SS |
45 | static inline int init_done(struct zram *zram) |
46 | { | |
47 | return zram->meta != NULL; | |
48 | } | |
49 | ||
9b3bb7ab SS |
50 | static inline struct zram *dev_to_zram(struct device *dev) |
51 | { | |
52 | return (struct zram *)dev_to_disk(dev)->private_data; | |
53 | } | |
54 | ||
55 | static ssize_t disksize_show(struct device *dev, | |
56 | struct device_attribute *attr, char *buf) | |
57 | { | |
58 | struct zram *zram = dev_to_zram(dev); | |
59 | ||
60 | return sprintf(buf, "%llu\n", zram->disksize); | |
61 | } | |
62 | ||
63 | static ssize_t initstate_show(struct device *dev, | |
64 | struct device_attribute *attr, char *buf) | |
65 | { | |
66 | struct zram *zram = dev_to_zram(dev); | |
67 | ||
be2d1d56 | 68 | return sprintf(buf, "%u\n", init_done(zram)); |
9b3bb7ab SS |
69 | } |
70 | ||
71 | static ssize_t num_reads_show(struct device *dev, | |
72 | struct device_attribute *attr, char *buf) | |
73 | { | |
74 | struct zram *zram = dev_to_zram(dev); | |
75 | ||
76 | return sprintf(buf, "%llu\n", | |
77 | (u64)atomic64_read(&zram->stats.num_reads)); | |
78 | } | |
79 | ||
80 | static ssize_t num_writes_show(struct device *dev, | |
81 | struct device_attribute *attr, char *buf) | |
82 | { | |
83 | struct zram *zram = dev_to_zram(dev); | |
84 | ||
85 | return sprintf(buf, "%llu\n", | |
86 | (u64)atomic64_read(&zram->stats.num_writes)); | |
87 | } | |
88 | ||
89 | static ssize_t invalid_io_show(struct device *dev, | |
90 | struct device_attribute *attr, char *buf) | |
91 | { | |
92 | struct zram *zram = dev_to_zram(dev); | |
93 | ||
94 | return sprintf(buf, "%llu\n", | |
95 | (u64)atomic64_read(&zram->stats.invalid_io)); | |
96 | } | |
97 | ||
98 | static ssize_t notify_free_show(struct device *dev, | |
99 | struct device_attribute *attr, char *buf) | |
100 | { | |
101 | struct zram *zram = dev_to_zram(dev); | |
102 | ||
103 | return sprintf(buf, "%llu\n", | |
104 | (u64)atomic64_read(&zram->stats.notify_free)); | |
105 | } | |
106 | ||
107 | static ssize_t zero_pages_show(struct device *dev, | |
108 | struct device_attribute *attr, char *buf) | |
109 | { | |
110 | struct zram *zram = dev_to_zram(dev); | |
111 | ||
deb0bdeb | 112 | return sprintf(buf, "%u\n", atomic_read(&zram->stats.pages_zero)); |
9b3bb7ab SS |
113 | } |
114 | ||
115 | static ssize_t orig_data_size_show(struct device *dev, | |
116 | struct device_attribute *attr, char *buf) | |
117 | { | |
118 | struct zram *zram = dev_to_zram(dev); | |
119 | ||
120 | return sprintf(buf, "%llu\n", | |
deb0bdeb | 121 | (u64)(atomic_read(&zram->stats.pages_stored)) << PAGE_SHIFT); |
9b3bb7ab SS |
122 | } |
123 | ||
124 | static ssize_t compr_data_size_show(struct device *dev, | |
125 | struct device_attribute *attr, char *buf) | |
126 | { | |
127 | struct zram *zram = dev_to_zram(dev); | |
128 | ||
129 | return sprintf(buf, "%llu\n", | |
130 | (u64)atomic64_read(&zram->stats.compr_size)); | |
131 | } | |
132 | ||
133 | static ssize_t mem_used_total_show(struct device *dev, | |
134 | struct device_attribute *attr, char *buf) | |
135 | { | |
136 | u64 val = 0; | |
137 | struct zram *zram = dev_to_zram(dev); | |
138 | struct zram_meta *meta = zram->meta; | |
139 | ||
140 | down_read(&zram->init_lock); | |
be2d1d56 | 141 | if (init_done(zram)) |
9b3bb7ab SS |
142 | val = zs_get_total_size_bytes(meta->mem_pool); |
143 | up_read(&zram->init_lock); | |
144 | ||
145 | return sprintf(buf, "%llu\n", val); | |
146 | } | |
147 | ||
92967471 | 148 | /* flag operations needs meta->tb_lock */ |
8b3cc3ed | 149 | static int zram_test_flag(struct zram_meta *meta, u32 index, |
f1e3cfff | 150 | enum zram_pageflags flag) |
306b0c95 | 151 | { |
8b3cc3ed | 152 | return meta->table[index].flags & BIT(flag); |
306b0c95 NG |
153 | } |
154 | ||
8b3cc3ed | 155 | static void zram_set_flag(struct zram_meta *meta, u32 index, |
f1e3cfff | 156 | enum zram_pageflags flag) |
306b0c95 | 157 | { |
8b3cc3ed | 158 | meta->table[index].flags |= BIT(flag); |
306b0c95 NG |
159 | } |
160 | ||
8b3cc3ed | 161 | static void zram_clear_flag(struct zram_meta *meta, u32 index, |
f1e3cfff | 162 | enum zram_pageflags flag) |
306b0c95 | 163 | { |
8b3cc3ed | 164 | meta->table[index].flags &= ~BIT(flag); |
306b0c95 NG |
165 | } |
166 | ||
9b3bb7ab SS |
167 | static inline int is_partial_io(struct bio_vec *bvec) |
168 | { | |
169 | return bvec->bv_len != PAGE_SIZE; | |
170 | } | |
171 | ||
172 | /* | |
173 | * Check if request is within bounds and aligned on zram logical blocks. | |
174 | */ | |
175 | static inline int valid_io_request(struct zram *zram, struct bio *bio) | |
176 | { | |
177 | u64 start, end, bound; | |
a539c72a | 178 | |
9b3bb7ab | 179 | /* unaligned request */ |
4f024f37 KO |
180 | if (unlikely(bio->bi_iter.bi_sector & |
181 | (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1))) | |
9b3bb7ab | 182 | return 0; |
4f024f37 | 183 | if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1))) |
9b3bb7ab SS |
184 | return 0; |
185 | ||
4f024f37 KO |
186 | start = bio->bi_iter.bi_sector; |
187 | end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT); | |
9b3bb7ab SS |
188 | bound = zram->disksize >> SECTOR_SHIFT; |
189 | /* out of range range */ | |
75c7caf5 | 190 | if (unlikely(start >= bound || end > bound || start > end)) |
9b3bb7ab SS |
191 | return 0; |
192 | ||
193 | /* I/O request is valid */ | |
194 | return 1; | |
195 | } | |
196 | ||
197 | static void zram_meta_free(struct zram_meta *meta) | |
198 | { | |
199 | zs_destroy_pool(meta->mem_pool); | |
200 | kfree(meta->compress_workmem); | |
201 | free_pages((unsigned long)meta->compress_buffer, 1); | |
202 | vfree(meta->table); | |
203 | kfree(meta); | |
204 | } | |
205 | ||
206 | static struct zram_meta *zram_meta_alloc(u64 disksize) | |
207 | { | |
208 | size_t num_pages; | |
209 | struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL); | |
210 | if (!meta) | |
211 | goto out; | |
212 | ||
213 | meta->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); | |
214 | if (!meta->compress_workmem) | |
215 | goto free_meta; | |
216 | ||
217 | meta->compress_buffer = | |
218 | (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); | |
219 | if (!meta->compress_buffer) { | |
220 | pr_err("Error allocating compressor buffer space\n"); | |
221 | goto free_workmem; | |
222 | } | |
223 | ||
224 | num_pages = disksize >> PAGE_SHIFT; | |
225 | meta->table = vzalloc(num_pages * sizeof(*meta->table)); | |
226 | if (!meta->table) { | |
227 | pr_err("Error allocating zram address table\n"); | |
228 | goto free_buffer; | |
229 | } | |
230 | ||
231 | meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM); | |
232 | if (!meta->mem_pool) { | |
233 | pr_err("Error creating memory pool\n"); | |
234 | goto free_table; | |
235 | } | |
236 | ||
92967471 | 237 | rwlock_init(&meta->tb_lock); |
e46e3315 | 238 | mutex_init(&meta->buffer_lock); |
9b3bb7ab SS |
239 | return meta; |
240 | ||
241 | free_table: | |
242 | vfree(meta->table); | |
243 | free_buffer: | |
244 | free_pages((unsigned long)meta->compress_buffer, 1); | |
245 | free_workmem: | |
246 | kfree(meta->compress_workmem); | |
247 | free_meta: | |
248 | kfree(meta); | |
249 | meta = NULL; | |
250 | out: | |
251 | return meta; | |
252 | } | |
253 | ||
254 | static void update_position(u32 *index, int *offset, struct bio_vec *bvec) | |
255 | { | |
256 | if (*offset + bvec->bv_len >= PAGE_SIZE) | |
257 | (*index)++; | |
258 | *offset = (*offset + bvec->bv_len) % PAGE_SIZE; | |
259 | } | |
260 | ||
306b0c95 NG |
261 | static int page_zero_filled(void *ptr) |
262 | { | |
263 | unsigned int pos; | |
264 | unsigned long *page; | |
265 | ||
266 | page = (unsigned long *)ptr; | |
267 | ||
268 | for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) { | |
269 | if (page[pos]) | |
270 | return 0; | |
271 | } | |
272 | ||
273 | return 1; | |
274 | } | |
275 | ||
9b3bb7ab SS |
276 | static void handle_zero_page(struct bio_vec *bvec) |
277 | { | |
278 | struct page *page = bvec->bv_page; | |
279 | void *user_mem; | |
280 | ||
281 | user_mem = kmap_atomic(page); | |
282 | if (is_partial_io(bvec)) | |
283 | memset(user_mem + bvec->bv_offset, 0, bvec->bv_len); | |
284 | else | |
285 | clear_page(user_mem); | |
286 | kunmap_atomic(user_mem); | |
287 | ||
288 | flush_dcache_page(page); | |
289 | } | |
290 | ||
92967471 | 291 | /* NOTE: caller should hold meta->tb_lock with write-side */ |
f1e3cfff | 292 | static void zram_free_page(struct zram *zram, size_t index) |
306b0c95 | 293 | { |
8b3cc3ed MK |
294 | struct zram_meta *meta = zram->meta; |
295 | unsigned long handle = meta->table[index].handle; | |
306b0c95 | 296 | |
fd1a30de | 297 | if (unlikely(!handle)) { |
2e882281 NG |
298 | /* |
299 | * No memory is allocated for zero filled pages. | |
300 | * Simply clear zero page flag. | |
301 | */ | |
8b3cc3ed MK |
302 | if (zram_test_flag(meta, index, ZRAM_ZERO)) { |
303 | zram_clear_flag(meta, index, ZRAM_ZERO); | |
deb0bdeb | 304 | atomic_dec(&zram->stats.pages_zero); |
306b0c95 NG |
305 | } |
306 | return; | |
307 | } | |
308 | ||
8b3cc3ed | 309 | zs_free(meta->mem_pool, handle); |
306b0c95 | 310 | |
da5cc7d3 | 311 | atomic64_sub(meta->table[index].size, &zram->stats.compr_size); |
deb0bdeb | 312 | atomic_dec(&zram->stats.pages_stored); |
306b0c95 | 313 | |
8b3cc3ed MK |
314 | meta->table[index].handle = 0; |
315 | meta->table[index].size = 0; | |
306b0c95 NG |
316 | } |
317 | ||
37b51fdd | 318 | static int zram_decompress_page(struct zram *zram, char *mem, u32 index) |
306b0c95 | 319 | { |
37b51fdd SS |
320 | int ret = LZO_E_OK; |
321 | size_t clen = PAGE_SIZE; | |
322 | unsigned char *cmem; | |
8b3cc3ed | 323 | struct zram_meta *meta = zram->meta; |
92967471 MK |
324 | unsigned long handle; |
325 | u16 size; | |
326 | ||
327 | read_lock(&meta->tb_lock); | |
328 | handle = meta->table[index].handle; | |
329 | size = meta->table[index].size; | |
306b0c95 | 330 | |
8b3cc3ed | 331 | if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) { |
92967471 | 332 | read_unlock(&meta->tb_lock); |
42e99bd9 | 333 | clear_page(mem); |
8c921b2b JM |
334 | return 0; |
335 | } | |
306b0c95 | 336 | |
8b3cc3ed | 337 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO); |
92967471 | 338 | if (size == PAGE_SIZE) |
42e99bd9 | 339 | copy_page(mem, cmem); |
37b51fdd | 340 | else |
92967471 | 341 | ret = lzo1x_decompress_safe(cmem, size, mem, &clen); |
8b3cc3ed | 342 | zs_unmap_object(meta->mem_pool, handle); |
92967471 | 343 | read_unlock(&meta->tb_lock); |
a1dd52af | 344 | |
8c921b2b JM |
345 | /* Should NEVER happen. Return bio error if it does. */ |
346 | if (unlikely(ret != LZO_E_OK)) { | |
347 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); | |
da5cc7d3 | 348 | atomic64_inc(&zram->stats.failed_reads); |
8c921b2b | 349 | return ret; |
a1dd52af | 350 | } |
306b0c95 | 351 | |
8c921b2b | 352 | return 0; |
306b0c95 NG |
353 | } |
354 | ||
37b51fdd SS |
355 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
356 | u32 index, int offset, struct bio *bio) | |
924bd88d JM |
357 | { |
358 | int ret; | |
37b51fdd SS |
359 | struct page *page; |
360 | unsigned char *user_mem, *uncmem = NULL; | |
8b3cc3ed | 361 | struct zram_meta *meta = zram->meta; |
37b51fdd SS |
362 | page = bvec->bv_page; |
363 | ||
92967471 | 364 | read_lock(&meta->tb_lock); |
8b3cc3ed MK |
365 | if (unlikely(!meta->table[index].handle) || |
366 | zram_test_flag(meta, index, ZRAM_ZERO)) { | |
92967471 | 367 | read_unlock(&meta->tb_lock); |
37b51fdd | 368 | handle_zero_page(bvec); |
924bd88d JM |
369 | return 0; |
370 | } | |
92967471 | 371 | read_unlock(&meta->tb_lock); |
924bd88d | 372 | |
37b51fdd SS |
373 | if (is_partial_io(bvec)) |
374 | /* Use a temporary buffer to decompress the page */ | |
7e5a5104 MK |
375 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
376 | ||
377 | user_mem = kmap_atomic(page); | |
378 | if (!is_partial_io(bvec)) | |
37b51fdd SS |
379 | uncmem = user_mem; |
380 | ||
381 | if (!uncmem) { | |
382 | pr_info("Unable to allocate temp memory\n"); | |
383 | ret = -ENOMEM; | |
384 | goto out_cleanup; | |
385 | } | |
924bd88d | 386 | |
37b51fdd | 387 | ret = zram_decompress_page(zram, uncmem, index); |
924bd88d | 388 | /* Should NEVER happen. Return bio error if it does. */ |
25eeb667 | 389 | if (unlikely(ret != LZO_E_OK)) |
37b51fdd | 390 | goto out_cleanup; |
924bd88d | 391 | |
37b51fdd SS |
392 | if (is_partial_io(bvec)) |
393 | memcpy(user_mem + bvec->bv_offset, uncmem + offset, | |
394 | bvec->bv_len); | |
395 | ||
396 | flush_dcache_page(page); | |
397 | ret = 0; | |
398 | out_cleanup: | |
399 | kunmap_atomic(user_mem); | |
400 | if (is_partial_io(bvec)) | |
401 | kfree(uncmem); | |
402 | return ret; | |
924bd88d JM |
403 | } |
404 | ||
405 | static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, | |
406 | int offset) | |
306b0c95 | 407 | { |
397c6066 | 408 | int ret = 0; |
8c921b2b | 409 | size_t clen; |
c2344348 | 410 | unsigned long handle; |
130f315a | 411 | struct page *page; |
924bd88d | 412 | unsigned char *user_mem, *cmem, *src, *uncmem = NULL; |
8b3cc3ed | 413 | struct zram_meta *meta = zram->meta; |
e46e3315 | 414 | bool locked = false; |
306b0c95 | 415 | |
8c921b2b | 416 | page = bvec->bv_page; |
8b3cc3ed | 417 | src = meta->compress_buffer; |
306b0c95 | 418 | |
924bd88d JM |
419 | if (is_partial_io(bvec)) { |
420 | /* | |
421 | * This is a partial IO. We need to read the full page | |
422 | * before to write the changes. | |
423 | */ | |
7e5a5104 | 424 | uncmem = kmalloc(PAGE_SIZE, GFP_NOIO); |
924bd88d | 425 | if (!uncmem) { |
924bd88d JM |
426 | ret = -ENOMEM; |
427 | goto out; | |
428 | } | |
37b51fdd | 429 | ret = zram_decompress_page(zram, uncmem, index); |
397c6066 | 430 | if (ret) |
924bd88d | 431 | goto out; |
924bd88d JM |
432 | } |
433 | ||
e46e3315 MK |
434 | mutex_lock(&meta->buffer_lock); |
435 | locked = true; | |
ba82fe2e | 436 | user_mem = kmap_atomic(page); |
924bd88d | 437 | |
397c6066 | 438 | if (is_partial_io(bvec)) { |
924bd88d JM |
439 | memcpy(uncmem + offset, user_mem + bvec->bv_offset, |
440 | bvec->bv_len); | |
397c6066 NG |
441 | kunmap_atomic(user_mem); |
442 | user_mem = NULL; | |
443 | } else { | |
924bd88d | 444 | uncmem = user_mem; |
397c6066 | 445 | } |
924bd88d JM |
446 | |
447 | if (page_zero_filled(uncmem)) { | |
ba82fe2e | 448 | kunmap_atomic(user_mem); |
f40ac2ae | 449 | /* Free memory associated with this sector now. */ |
92967471 | 450 | write_lock(&zram->meta->tb_lock); |
f40ac2ae | 451 | zram_free_page(zram, index); |
92967471 MK |
452 | zram_set_flag(meta, index, ZRAM_ZERO); |
453 | write_unlock(&zram->meta->tb_lock); | |
f40ac2ae | 454 | |
deb0bdeb | 455 | atomic_inc(&zram->stats.pages_zero); |
924bd88d JM |
456 | ret = 0; |
457 | goto out; | |
8c921b2b | 458 | } |
306b0c95 | 459 | |
924bd88d | 460 | ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen, |
8b3cc3ed | 461 | meta->compress_workmem); |
397c6066 NG |
462 | if (!is_partial_io(bvec)) { |
463 | kunmap_atomic(user_mem); | |
464 | user_mem = NULL; | |
465 | uncmem = NULL; | |
466 | } | |
306b0c95 | 467 | |
8c921b2b | 468 | if (unlikely(ret != LZO_E_OK)) { |
8c921b2b | 469 | pr_err("Compression failed! err=%d\n", ret); |
924bd88d | 470 | goto out; |
8c921b2b | 471 | } |
306b0c95 | 472 | |
c8f2f0db | 473 | if (unlikely(clen > max_zpage_size)) { |
c8f2f0db | 474 | clen = PAGE_SIZE; |
397c6066 NG |
475 | src = NULL; |
476 | if (is_partial_io(bvec)) | |
477 | src = uncmem; | |
c8f2f0db | 478 | } |
a1dd52af | 479 | |
8b3cc3ed | 480 | handle = zs_malloc(meta->mem_pool, clen); |
fd1a30de | 481 | if (!handle) { |
596b3dd4 MR |
482 | pr_info("Error allocating memory for compressed page: %u, size=%zu\n", |
483 | index, clen); | |
924bd88d JM |
484 | ret = -ENOMEM; |
485 | goto out; | |
8c921b2b | 486 | } |
8b3cc3ed | 487 | cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO); |
306b0c95 | 488 | |
42e99bd9 | 489 | if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) { |
397c6066 | 490 | src = kmap_atomic(page); |
42e99bd9 | 491 | copy_page(cmem, src); |
397c6066 | 492 | kunmap_atomic(src); |
42e99bd9 JL |
493 | } else { |
494 | memcpy(cmem, src, clen); | |
495 | } | |
306b0c95 | 496 | |
8b3cc3ed | 497 | zs_unmap_object(meta->mem_pool, handle); |
fd1a30de | 498 | |
f40ac2ae SS |
499 | /* |
500 | * Free memory associated with this sector | |
501 | * before overwriting unused sectors. | |
502 | */ | |
92967471 | 503 | write_lock(&zram->meta->tb_lock); |
f40ac2ae SS |
504 | zram_free_page(zram, index); |
505 | ||
8b3cc3ed MK |
506 | meta->table[index].handle = handle; |
507 | meta->table[index].size = clen; | |
92967471 | 508 | write_unlock(&zram->meta->tb_lock); |
306b0c95 | 509 | |
8c921b2b | 510 | /* Update stats */ |
da5cc7d3 | 511 | atomic64_add(clen, &zram->stats.compr_size); |
deb0bdeb | 512 | atomic_inc(&zram->stats.pages_stored); |
924bd88d | 513 | out: |
e46e3315 MK |
514 | if (locked) |
515 | mutex_unlock(&meta->buffer_lock); | |
397c6066 NG |
516 | if (is_partial_io(bvec)) |
517 | kfree(uncmem); | |
518 | ||
924bd88d | 519 | if (ret) |
da5cc7d3 | 520 | atomic64_inc(&zram->stats.failed_writes); |
924bd88d | 521 | return ret; |
8c921b2b JM |
522 | } |
523 | ||
524 | static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, | |
be257c61 | 525 | int offset, struct bio *bio) |
8c921b2b | 526 | { |
c5bde238 | 527 | int ret; |
be257c61 | 528 | int rw = bio_data_dir(bio); |
8c921b2b | 529 | |
be257c61 SS |
530 | if (rw == READ) { |
531 | atomic64_inc(&zram->stats.num_reads); | |
c5bde238 | 532 | ret = zram_bvec_read(zram, bvec, index, offset, bio); |
be257c61 SS |
533 | } else { |
534 | atomic64_inc(&zram->stats.num_writes); | |
c5bde238 | 535 | ret = zram_bvec_write(zram, bvec, index, offset); |
be257c61 | 536 | } |
c5bde238 JM |
537 | |
538 | return ret; | |
924bd88d JM |
539 | } |
540 | ||
2b86ab9c | 541 | static void zram_reset_device(struct zram *zram, bool reset_capacity) |
924bd88d | 542 | { |
9b3bb7ab SS |
543 | size_t index; |
544 | struct zram_meta *meta; | |
545 | ||
644d4787 | 546 | down_write(&zram->init_lock); |
be2d1d56 | 547 | if (!init_done(zram)) { |
644d4787 | 548 | up_write(&zram->init_lock); |
9b3bb7ab | 549 | return; |
644d4787 | 550 | } |
9b3bb7ab SS |
551 | |
552 | meta = zram->meta; | |
9b3bb7ab SS |
553 | /* Free all pages that are still in this zram device */ |
554 | for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) { | |
555 | unsigned long handle = meta->table[index].handle; | |
556 | if (!handle) | |
557 | continue; | |
558 | ||
559 | zs_free(meta->mem_pool, handle); | |
560 | } | |
561 | ||
562 | zram_meta_free(zram->meta); | |
563 | zram->meta = NULL; | |
564 | /* Reset stats */ | |
565 | memset(&zram->stats, 0, sizeof(zram->stats)); | |
566 | ||
567 | zram->disksize = 0; | |
2b86ab9c MK |
568 | if (reset_capacity) |
569 | set_capacity(zram->disk, 0); | |
644d4787 | 570 | up_write(&zram->init_lock); |
9b3bb7ab SS |
571 | } |
572 | ||
573 | static void zram_init_device(struct zram *zram, struct zram_meta *meta) | |
574 | { | |
575 | if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) { | |
576 | pr_info( | |
577 | "There is little point creating a zram of greater than " | |
578 | "twice the size of memory since we expect a 2:1 compression " | |
579 | "ratio. Note that zram uses about 0.1%% of the size of " | |
580 | "the disk when not in use so a huge zram is " | |
581 | "wasteful.\n" | |
582 | "\tMemory Size: %lu kB\n" | |
583 | "\tSize you selected: %llu kB\n" | |
584 | "Continuing anyway ...\n", | |
585 | (totalram_pages << PAGE_SHIFT) >> 10, zram->disksize >> 10 | |
586 | ); | |
587 | } | |
588 | ||
589 | /* zram devices sort of resembles non-rotational disks */ | |
590 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); | |
591 | ||
592 | zram->meta = meta; | |
9b3bb7ab SS |
593 | pr_debug("Initialization done!\n"); |
594 | } | |
595 | ||
596 | static ssize_t disksize_store(struct device *dev, | |
597 | struct device_attribute *attr, const char *buf, size_t len) | |
598 | { | |
599 | u64 disksize; | |
600 | struct zram_meta *meta; | |
601 | struct zram *zram = dev_to_zram(dev); | |
602 | ||
603 | disksize = memparse(buf, NULL); | |
604 | if (!disksize) | |
605 | return -EINVAL; | |
606 | ||
607 | disksize = PAGE_ALIGN(disksize); | |
608 | meta = zram_meta_alloc(disksize); | |
db5d711e MK |
609 | if (!meta) |
610 | return -ENOMEM; | |
9b3bb7ab | 611 | down_write(&zram->init_lock); |
be2d1d56 | 612 | if (init_done(zram)) { |
9b3bb7ab SS |
613 | up_write(&zram->init_lock); |
614 | zram_meta_free(meta); | |
615 | pr_info("Cannot change disksize for initialized device\n"); | |
616 | return -EBUSY; | |
617 | } | |
618 | ||
619 | zram->disksize = disksize; | |
620 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); | |
621 | zram_init_device(zram, meta); | |
622 | up_write(&zram->init_lock); | |
623 | ||
624 | return len; | |
625 | } | |
626 | ||
627 | static ssize_t reset_store(struct device *dev, | |
628 | struct device_attribute *attr, const char *buf, size_t len) | |
629 | { | |
630 | int ret; | |
631 | unsigned short do_reset; | |
632 | struct zram *zram; | |
633 | struct block_device *bdev; | |
634 | ||
635 | zram = dev_to_zram(dev); | |
636 | bdev = bdget_disk(zram->disk, 0); | |
637 | ||
46a51c80 RK |
638 | if (!bdev) |
639 | return -ENOMEM; | |
640 | ||
9b3bb7ab | 641 | /* Do not reset an active device! */ |
1b672224 RK |
642 | if (bdev->bd_holders) { |
643 | ret = -EBUSY; | |
644 | goto out; | |
645 | } | |
9b3bb7ab SS |
646 | |
647 | ret = kstrtou16(buf, 10, &do_reset); | |
648 | if (ret) | |
1b672224 | 649 | goto out; |
9b3bb7ab | 650 | |
1b672224 RK |
651 | if (!do_reset) { |
652 | ret = -EINVAL; | |
653 | goto out; | |
654 | } | |
9b3bb7ab SS |
655 | |
656 | /* Make sure all pending I/O is finished */ | |
46a51c80 | 657 | fsync_bdev(bdev); |
1b672224 | 658 | bdput(bdev); |
9b3bb7ab | 659 | |
2b86ab9c | 660 | zram_reset_device(zram, true); |
9b3bb7ab | 661 | return len; |
1b672224 RK |
662 | |
663 | out: | |
664 | bdput(bdev); | |
665 | return ret; | |
8c921b2b JM |
666 | } |
667 | ||
be257c61 | 668 | static void __zram_make_request(struct zram *zram, struct bio *bio) |
8c921b2b | 669 | { |
7988613b | 670 | int offset; |
8c921b2b | 671 | u32 index; |
7988613b KO |
672 | struct bio_vec bvec; |
673 | struct bvec_iter iter; | |
8c921b2b | 674 | |
4f024f37 KO |
675 | index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT; |
676 | offset = (bio->bi_iter.bi_sector & | |
677 | (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; | |
8c921b2b | 678 | |
7988613b | 679 | bio_for_each_segment(bvec, bio, iter) { |
924bd88d JM |
680 | int max_transfer_size = PAGE_SIZE - offset; |
681 | ||
7988613b | 682 | if (bvec.bv_len > max_transfer_size) { |
924bd88d JM |
683 | /* |
684 | * zram_bvec_rw() can only make operation on a single | |
685 | * zram page. Split the bio vector. | |
686 | */ | |
687 | struct bio_vec bv; | |
688 | ||
7988613b | 689 | bv.bv_page = bvec.bv_page; |
924bd88d | 690 | bv.bv_len = max_transfer_size; |
7988613b | 691 | bv.bv_offset = bvec.bv_offset; |
924bd88d | 692 | |
be257c61 | 693 | if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0) |
924bd88d JM |
694 | goto out; |
695 | ||
7988613b | 696 | bv.bv_len = bvec.bv_len - max_transfer_size; |
924bd88d | 697 | bv.bv_offset += max_transfer_size; |
be257c61 | 698 | if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0) |
924bd88d JM |
699 | goto out; |
700 | } else | |
be257c61 | 701 | if (zram_bvec_rw(zram, &bvec, index, offset, bio) < 0) |
924bd88d JM |
702 | goto out; |
703 | ||
7988613b | 704 | update_position(&index, &offset, &bvec); |
a1dd52af | 705 | } |
306b0c95 NG |
706 | |
707 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
708 | bio_endio(bio, 0); | |
7d7854b4 | 709 | return; |
306b0c95 NG |
710 | |
711 | out: | |
306b0c95 | 712 | bio_io_error(bio); |
306b0c95 NG |
713 | } |
714 | ||
306b0c95 | 715 | /* |
f1e3cfff | 716 | * Handler function for all zram I/O requests. |
306b0c95 | 717 | */ |
5a7bbad2 | 718 | static void zram_make_request(struct request_queue *queue, struct bio *bio) |
306b0c95 | 719 | { |
f1e3cfff | 720 | struct zram *zram = queue->queuedata; |
306b0c95 | 721 | |
0900beae | 722 | down_read(&zram->init_lock); |
be2d1d56 | 723 | if (unlikely(!init_done(zram))) |
3de738cd | 724 | goto error; |
0900beae | 725 | |
f1e3cfff | 726 | if (!valid_io_request(zram, bio)) { |
da5cc7d3 | 727 | atomic64_inc(&zram->stats.invalid_io); |
3de738cd | 728 | goto error; |
6642a67c JM |
729 | } |
730 | ||
be257c61 | 731 | __zram_make_request(zram, bio); |
0900beae | 732 | up_read(&zram->init_lock); |
306b0c95 | 733 | |
b4fdcb02 | 734 | return; |
0900beae | 735 | |
0900beae | 736 | error: |
3de738cd | 737 | up_read(&zram->init_lock); |
0900beae | 738 | bio_io_error(bio); |
306b0c95 NG |
739 | } |
740 | ||
2ccbec05 NG |
741 | static void zram_slot_free_notify(struct block_device *bdev, |
742 | unsigned long index) | |
107c161b | 743 | { |
f1e3cfff | 744 | struct zram *zram; |
f614a9f4 | 745 | struct zram_meta *meta; |
107c161b | 746 | |
f1e3cfff | 747 | zram = bdev->bd_disk->private_data; |
f614a9f4 | 748 | meta = zram->meta; |
a0c516cb | 749 | |
f614a9f4 MK |
750 | write_lock(&meta->tb_lock); |
751 | zram_free_page(zram, index); | |
752 | write_unlock(&meta->tb_lock); | |
753 | atomic64_inc(&zram->stats.notify_free); | |
107c161b NG |
754 | } |
755 | ||
f1e3cfff | 756 | static const struct block_device_operations zram_devops = { |
f1e3cfff | 757 | .swap_slot_free_notify = zram_slot_free_notify, |
107c161b | 758 | .owner = THIS_MODULE |
306b0c95 NG |
759 | }; |
760 | ||
9b3bb7ab SS |
761 | static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR, |
762 | disksize_show, disksize_store); | |
763 | static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL); | |
764 | static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store); | |
765 | static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL); | |
766 | static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL); | |
767 | static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL); | |
768 | static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL); | |
769 | static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL); | |
770 | static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL); | |
771 | static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL); | |
772 | static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL); | |
773 | ||
774 | static struct attribute *zram_disk_attrs[] = { | |
775 | &dev_attr_disksize.attr, | |
776 | &dev_attr_initstate.attr, | |
777 | &dev_attr_reset.attr, | |
778 | &dev_attr_num_reads.attr, | |
779 | &dev_attr_num_writes.attr, | |
780 | &dev_attr_invalid_io.attr, | |
781 | &dev_attr_notify_free.attr, | |
782 | &dev_attr_zero_pages.attr, | |
783 | &dev_attr_orig_data_size.attr, | |
784 | &dev_attr_compr_data_size.attr, | |
785 | &dev_attr_mem_used_total.attr, | |
786 | NULL, | |
787 | }; | |
788 | ||
789 | static struct attribute_group zram_disk_attr_group = { | |
790 | .attrs = zram_disk_attrs, | |
791 | }; | |
792 | ||
f1e3cfff | 793 | static int create_device(struct zram *zram, int device_id) |
306b0c95 | 794 | { |
39a9b8ac | 795 | int ret = -ENOMEM; |
de1a21a0 | 796 | |
0900beae | 797 | init_rwsem(&zram->init_lock); |
306b0c95 | 798 | |
f1e3cfff NG |
799 | zram->queue = blk_alloc_queue(GFP_KERNEL); |
800 | if (!zram->queue) { | |
306b0c95 NG |
801 | pr_err("Error allocating disk queue for device %d\n", |
802 | device_id); | |
de1a21a0 | 803 | goto out; |
306b0c95 NG |
804 | } |
805 | ||
f1e3cfff NG |
806 | blk_queue_make_request(zram->queue, zram_make_request); |
807 | zram->queue->queuedata = zram; | |
306b0c95 NG |
808 | |
809 | /* gendisk structure */ | |
f1e3cfff NG |
810 | zram->disk = alloc_disk(1); |
811 | if (!zram->disk) { | |
94b8435f | 812 | pr_warn("Error allocating disk structure for device %d\n", |
306b0c95 | 813 | device_id); |
39a9b8ac | 814 | goto out_free_queue; |
306b0c95 NG |
815 | } |
816 | ||
f1e3cfff NG |
817 | zram->disk->major = zram_major; |
818 | zram->disk->first_minor = device_id; | |
819 | zram->disk->fops = &zram_devops; | |
820 | zram->disk->queue = zram->queue; | |
821 | zram->disk->private_data = zram; | |
822 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); | |
306b0c95 | 823 | |
33863c21 | 824 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
f1e3cfff | 825 | set_capacity(zram->disk, 0); |
5d83d5a0 | 826 | |
a1dd52af NG |
827 | /* |
828 | * To ensure that we always get PAGE_SIZE aligned | |
829 | * and n*PAGE_SIZED sized I/O requests. | |
830 | */ | |
f1e3cfff | 831 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
7b19b8d4 RJ |
832 | blk_queue_logical_block_size(zram->disk->queue, |
833 | ZRAM_LOGICAL_BLOCK_SIZE); | |
f1e3cfff NG |
834 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
835 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); | |
5d83d5a0 | 836 | |
f1e3cfff | 837 | add_disk(zram->disk); |
306b0c95 | 838 | |
33863c21 NG |
839 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
840 | &zram_disk_attr_group); | |
841 | if (ret < 0) { | |
94b8435f | 842 | pr_warn("Error creating sysfs group"); |
39a9b8ac | 843 | goto out_free_disk; |
33863c21 | 844 | } |
33863c21 | 845 | |
be2d1d56 | 846 | zram->meta = NULL; |
39a9b8ac | 847 | return 0; |
de1a21a0 | 848 | |
39a9b8ac JL |
849 | out_free_disk: |
850 | del_gendisk(zram->disk); | |
851 | put_disk(zram->disk); | |
852 | out_free_queue: | |
853 | blk_cleanup_queue(zram->queue); | |
de1a21a0 NG |
854 | out: |
855 | return ret; | |
306b0c95 NG |
856 | } |
857 | ||
f1e3cfff | 858 | static void destroy_device(struct zram *zram) |
306b0c95 | 859 | { |
33863c21 NG |
860 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, |
861 | &zram_disk_attr_group); | |
33863c21 | 862 | |
59d3fe54 RK |
863 | del_gendisk(zram->disk); |
864 | put_disk(zram->disk); | |
306b0c95 | 865 | |
59d3fe54 | 866 | blk_cleanup_queue(zram->queue); |
306b0c95 NG |
867 | } |
868 | ||
f1e3cfff | 869 | static int __init zram_init(void) |
306b0c95 | 870 | { |
de1a21a0 | 871 | int ret, dev_id; |
306b0c95 | 872 | |
5fa5a901 | 873 | if (num_devices > max_num_devices) { |
94b8435f | 874 | pr_warn("Invalid value for num_devices: %u\n", |
5fa5a901 | 875 | num_devices); |
de1a21a0 NG |
876 | ret = -EINVAL; |
877 | goto out; | |
306b0c95 NG |
878 | } |
879 | ||
f1e3cfff NG |
880 | zram_major = register_blkdev(0, "zram"); |
881 | if (zram_major <= 0) { | |
94b8435f | 882 | pr_warn("Unable to get major number\n"); |
de1a21a0 NG |
883 | ret = -EBUSY; |
884 | goto out; | |
306b0c95 NG |
885 | } |
886 | ||
306b0c95 | 887 | /* Allocate the device array and initialize each one */ |
5fa5a901 | 888 | zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL); |
43801f6e | 889 | if (!zram_devices) { |
de1a21a0 NG |
890 | ret = -ENOMEM; |
891 | goto unregister; | |
892 | } | |
306b0c95 | 893 | |
5fa5a901 | 894 | for (dev_id = 0; dev_id < num_devices; dev_id++) { |
43801f6e | 895 | ret = create_device(&zram_devices[dev_id], dev_id); |
de1a21a0 | 896 | if (ret) |
3bf040c7 | 897 | goto free_devices; |
de1a21a0 NG |
898 | } |
899 | ||
ca3d70bd DB |
900 | pr_info("Created %u device(s) ...\n", num_devices); |
901 | ||
306b0c95 | 902 | return 0; |
de1a21a0 | 903 | |
3bf040c7 | 904 | free_devices: |
de1a21a0 | 905 | while (dev_id) |
43801f6e NW |
906 | destroy_device(&zram_devices[--dev_id]); |
907 | kfree(zram_devices); | |
de1a21a0 | 908 | unregister: |
f1e3cfff | 909 | unregister_blkdev(zram_major, "zram"); |
de1a21a0 | 910 | out: |
306b0c95 NG |
911 | return ret; |
912 | } | |
913 | ||
f1e3cfff | 914 | static void __exit zram_exit(void) |
306b0c95 NG |
915 | { |
916 | int i; | |
f1e3cfff | 917 | struct zram *zram; |
306b0c95 | 918 | |
5fa5a901 | 919 | for (i = 0; i < num_devices; i++) { |
43801f6e | 920 | zram = &zram_devices[i]; |
306b0c95 | 921 | |
f1e3cfff | 922 | destroy_device(zram); |
2b86ab9c MK |
923 | /* |
924 | * Shouldn't access zram->disk after destroy_device | |
925 | * because destroy_device already released zram->disk. | |
926 | */ | |
927 | zram_reset_device(zram, false); | |
306b0c95 NG |
928 | } |
929 | ||
f1e3cfff | 930 | unregister_blkdev(zram_major, "zram"); |
306b0c95 | 931 | |
43801f6e | 932 | kfree(zram_devices); |
306b0c95 NG |
933 | pr_debug("Cleanup done!\n"); |
934 | } | |
935 | ||
f1e3cfff NG |
936 | module_init(zram_init); |
937 | module_exit(zram_exit); | |
306b0c95 | 938 | |
9b3bb7ab SS |
939 | module_param(num_devices, uint, 0); |
940 | MODULE_PARM_DESC(num_devices, "Number of zram devices"); | |
941 | ||
306b0c95 NG |
942 | MODULE_LICENSE("Dual BSD/GPL"); |
943 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); | |
f1e3cfff | 944 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |