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