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