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