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