]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/block/zram/zram_drv.c
4479011bfc432475fb4a5a7e3b75e86d5a094dfd
[mirror_ubuntu-hirsute-kernel.git] / drivers / block / zram / zram_drv.c
1 /*
2 * Compressed RAM block device
3 *
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
5 * 2012, 2013 Minchan Kim
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 *
13 */
14
15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/bitops.h>
22 #include <linux/blkdev.h>
23 #include <linux/buffer_head.h>
24 #include <linux/device.h>
25 #include <linux/genhd.h>
26 #include <linux/highmem.h>
27 #include <linux/slab.h>
28 #include <linux/backing-dev.h>
29 #include <linux/string.h>
30 #include <linux/vmalloc.h>
31 #include <linux/err.h>
32 #include <linux/idr.h>
33 #include <linux/sysfs.h>
34 #include <linux/debugfs.h>
35 #include <linux/cpuhotplug.h>
36 #include <linux/part_stat.h>
37
38 #include "zram_drv.h"
39
40 static DEFINE_IDR(zram_index_idr);
41 /* idr index must be protected */
42 static DEFINE_MUTEX(zram_index_mutex);
43
44 static int zram_major;
45 static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
46
47 /* Module params (documentation at end) */
48 static unsigned int num_devices = 1;
49 /*
50 * Pages that compress to sizes equals or greater than this are stored
51 * uncompressed in memory.
52 */
53 static size_t huge_class_size;
54
55 static const struct block_device_operations zram_devops;
56 static const struct block_device_operations zram_wb_devops;
57
58 static void zram_free_page(struct zram *zram, size_t index);
59 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
60 u32 index, int offset, struct bio *bio);
61
62
63 static int zram_slot_trylock(struct zram *zram, u32 index)
64 {
65 return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags);
66 }
67
68 static void zram_slot_lock(struct zram *zram, u32 index)
69 {
70 bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags);
71 }
72
73 static void zram_slot_unlock(struct zram *zram, u32 index)
74 {
75 bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags);
76 }
77
78 static inline bool init_done(struct zram *zram)
79 {
80 return zram->disksize;
81 }
82
83 static inline struct zram *dev_to_zram(struct device *dev)
84 {
85 return (struct zram *)dev_to_disk(dev)->private_data;
86 }
87
88 static unsigned long zram_get_handle(struct zram *zram, u32 index)
89 {
90 return zram->table[index].handle;
91 }
92
93 static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
94 {
95 zram->table[index].handle = handle;
96 }
97
98 /* flag operations require table entry bit_spin_lock() being held */
99 static bool zram_test_flag(struct zram *zram, u32 index,
100 enum zram_pageflags flag)
101 {
102 return zram->table[index].flags & BIT(flag);
103 }
104
105 static void zram_set_flag(struct zram *zram, u32 index,
106 enum zram_pageflags flag)
107 {
108 zram->table[index].flags |= BIT(flag);
109 }
110
111 static void zram_clear_flag(struct zram *zram, u32 index,
112 enum zram_pageflags flag)
113 {
114 zram->table[index].flags &= ~BIT(flag);
115 }
116
117 static inline void zram_set_element(struct zram *zram, u32 index,
118 unsigned long element)
119 {
120 zram->table[index].element = element;
121 }
122
123 static unsigned long zram_get_element(struct zram *zram, u32 index)
124 {
125 return zram->table[index].element;
126 }
127
128 static size_t zram_get_obj_size(struct zram *zram, u32 index)
129 {
130 return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
131 }
132
133 static void zram_set_obj_size(struct zram *zram,
134 u32 index, size_t size)
135 {
136 unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
137
138 zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
139 }
140
141 static inline bool zram_allocated(struct zram *zram, u32 index)
142 {
143 return zram_get_obj_size(zram, index) ||
144 zram_test_flag(zram, index, ZRAM_SAME) ||
145 zram_test_flag(zram, index, ZRAM_WB);
146 }
147
148 #if PAGE_SIZE != 4096
149 static inline bool is_partial_io(struct bio_vec *bvec)
150 {
151 return bvec->bv_len != PAGE_SIZE;
152 }
153 #else
154 static inline bool is_partial_io(struct bio_vec *bvec)
155 {
156 return false;
157 }
158 #endif
159
160 /*
161 * Check if request is within bounds and aligned on zram logical blocks.
162 */
163 static inline bool valid_io_request(struct zram *zram,
164 sector_t start, unsigned int size)
165 {
166 u64 end, bound;
167
168 /* unaligned request */
169 if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
170 return false;
171 if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
172 return false;
173
174 end = start + (size >> SECTOR_SHIFT);
175 bound = zram->disksize >> SECTOR_SHIFT;
176 /* out of range range */
177 if (unlikely(start >= bound || end > bound || start > end))
178 return false;
179
180 /* I/O request is valid */
181 return true;
182 }
183
184 static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
185 {
186 *index += (*offset + bvec->bv_len) / PAGE_SIZE;
187 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
188 }
189
190 static inline void update_used_max(struct zram *zram,
191 const unsigned long pages)
192 {
193 unsigned long old_max, cur_max;
194
195 old_max = atomic_long_read(&zram->stats.max_used_pages);
196
197 do {
198 cur_max = old_max;
199 if (pages > cur_max)
200 old_max = atomic_long_cmpxchg(
201 &zram->stats.max_used_pages, cur_max, pages);
202 } while (old_max != cur_max);
203 }
204
205 static inline void zram_fill_page(void *ptr, unsigned long len,
206 unsigned long value)
207 {
208 WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
209 memset_l(ptr, value, len / sizeof(unsigned long));
210 }
211
212 static bool page_same_filled(void *ptr, unsigned long *element)
213 {
214 unsigned long *page;
215 unsigned long val;
216 unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
217
218 page = (unsigned long *)ptr;
219 val = page[0];
220
221 if (val != page[last_pos])
222 return false;
223
224 for (pos = 1; pos < last_pos; pos++) {
225 if (val != page[pos])
226 return false;
227 }
228
229 *element = val;
230
231 return true;
232 }
233
234 static ssize_t initstate_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
236 {
237 u32 val;
238 struct zram *zram = dev_to_zram(dev);
239
240 down_read(&zram->init_lock);
241 val = init_done(zram);
242 up_read(&zram->init_lock);
243
244 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
245 }
246
247 static ssize_t disksize_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
249 {
250 struct zram *zram = dev_to_zram(dev);
251
252 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
253 }
254
255 static ssize_t mem_limit_store(struct device *dev,
256 struct device_attribute *attr, const char *buf, size_t len)
257 {
258 u64 limit;
259 char *tmp;
260 struct zram *zram = dev_to_zram(dev);
261
262 limit = memparse(buf, &tmp);
263 if (buf == tmp) /* no chars parsed, invalid input */
264 return -EINVAL;
265
266 down_write(&zram->init_lock);
267 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
268 up_write(&zram->init_lock);
269
270 return len;
271 }
272
273 static ssize_t mem_used_max_store(struct device *dev,
274 struct device_attribute *attr, const char *buf, size_t len)
275 {
276 int err;
277 unsigned long val;
278 struct zram *zram = dev_to_zram(dev);
279
280 err = kstrtoul(buf, 10, &val);
281 if (err || val != 0)
282 return -EINVAL;
283
284 down_read(&zram->init_lock);
285 if (init_done(zram)) {
286 atomic_long_set(&zram->stats.max_used_pages,
287 zs_get_total_pages(zram->mem_pool));
288 }
289 up_read(&zram->init_lock);
290
291 return len;
292 }
293
294 static ssize_t idle_store(struct device *dev,
295 struct device_attribute *attr, const char *buf, size_t len)
296 {
297 struct zram *zram = dev_to_zram(dev);
298 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
299 int index;
300
301 if (!sysfs_streq(buf, "all"))
302 return -EINVAL;
303
304 down_read(&zram->init_lock);
305 if (!init_done(zram)) {
306 up_read(&zram->init_lock);
307 return -EINVAL;
308 }
309
310 for (index = 0; index < nr_pages; index++) {
311 /*
312 * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
313 * See the comment in writeback_store.
314 */
315 zram_slot_lock(zram, index);
316 if (zram_allocated(zram, index) &&
317 !zram_test_flag(zram, index, ZRAM_UNDER_WB))
318 zram_set_flag(zram, index, ZRAM_IDLE);
319 zram_slot_unlock(zram, index);
320 }
321
322 up_read(&zram->init_lock);
323
324 return len;
325 }
326
327 #ifdef CONFIG_ZRAM_WRITEBACK
328 static ssize_t writeback_limit_enable_store(struct device *dev,
329 struct device_attribute *attr, const char *buf, size_t len)
330 {
331 struct zram *zram = dev_to_zram(dev);
332 u64 val;
333 ssize_t ret = -EINVAL;
334
335 if (kstrtoull(buf, 10, &val))
336 return ret;
337
338 down_read(&zram->init_lock);
339 spin_lock(&zram->wb_limit_lock);
340 zram->wb_limit_enable = val;
341 spin_unlock(&zram->wb_limit_lock);
342 up_read(&zram->init_lock);
343 ret = len;
344
345 return ret;
346 }
347
348 static ssize_t writeback_limit_enable_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350 {
351 bool val;
352 struct zram *zram = dev_to_zram(dev);
353
354 down_read(&zram->init_lock);
355 spin_lock(&zram->wb_limit_lock);
356 val = zram->wb_limit_enable;
357 spin_unlock(&zram->wb_limit_lock);
358 up_read(&zram->init_lock);
359
360 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
361 }
362
363 static ssize_t writeback_limit_store(struct device *dev,
364 struct device_attribute *attr, const char *buf, size_t len)
365 {
366 struct zram *zram = dev_to_zram(dev);
367 u64 val;
368 ssize_t ret = -EINVAL;
369
370 if (kstrtoull(buf, 10, &val))
371 return ret;
372
373 down_read(&zram->init_lock);
374 spin_lock(&zram->wb_limit_lock);
375 zram->bd_wb_limit = val;
376 spin_unlock(&zram->wb_limit_lock);
377 up_read(&zram->init_lock);
378 ret = len;
379
380 return ret;
381 }
382
383 static ssize_t writeback_limit_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
385 {
386 u64 val;
387 struct zram *zram = dev_to_zram(dev);
388
389 down_read(&zram->init_lock);
390 spin_lock(&zram->wb_limit_lock);
391 val = zram->bd_wb_limit;
392 spin_unlock(&zram->wb_limit_lock);
393 up_read(&zram->init_lock);
394
395 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
396 }
397
398 static void reset_bdev(struct zram *zram)
399 {
400 struct block_device *bdev;
401
402 if (!zram->backing_dev)
403 return;
404
405 bdev = zram->bdev;
406 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
407 /* hope filp_close flush all of IO */
408 filp_close(zram->backing_dev, NULL);
409 zram->backing_dev = NULL;
410 zram->bdev = NULL;
411 zram->disk->fops = &zram_devops;
412 kvfree(zram->bitmap);
413 zram->bitmap = NULL;
414 }
415
416 static ssize_t backing_dev_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
418 {
419 struct file *file;
420 struct zram *zram = dev_to_zram(dev);
421 char *p;
422 ssize_t ret;
423
424 down_read(&zram->init_lock);
425 file = zram->backing_dev;
426 if (!file) {
427 memcpy(buf, "none\n", 5);
428 up_read(&zram->init_lock);
429 return 5;
430 }
431
432 p = file_path(file, buf, PAGE_SIZE - 1);
433 if (IS_ERR(p)) {
434 ret = PTR_ERR(p);
435 goto out;
436 }
437
438 ret = strlen(p);
439 memmove(buf, p, ret);
440 buf[ret++] = '\n';
441 out:
442 up_read(&zram->init_lock);
443 return ret;
444 }
445
446 static ssize_t backing_dev_store(struct device *dev,
447 struct device_attribute *attr, const char *buf, size_t len)
448 {
449 char *file_name;
450 size_t sz;
451 struct file *backing_dev = NULL;
452 struct inode *inode;
453 struct address_space *mapping;
454 unsigned int bitmap_sz;
455 unsigned long nr_pages, *bitmap = NULL;
456 struct block_device *bdev = NULL;
457 int err;
458 struct zram *zram = dev_to_zram(dev);
459
460 file_name = kmalloc(PATH_MAX, GFP_KERNEL);
461 if (!file_name)
462 return -ENOMEM;
463
464 down_write(&zram->init_lock);
465 if (init_done(zram)) {
466 pr_info("Can't setup backing device for initialized device\n");
467 err = -EBUSY;
468 goto out;
469 }
470
471 strlcpy(file_name, buf, PATH_MAX);
472 /* ignore trailing newline */
473 sz = strlen(file_name);
474 if (sz > 0 && file_name[sz - 1] == '\n')
475 file_name[sz - 1] = 0x00;
476
477 backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
478 if (IS_ERR(backing_dev)) {
479 err = PTR_ERR(backing_dev);
480 backing_dev = NULL;
481 goto out;
482 }
483
484 mapping = backing_dev->f_mapping;
485 inode = mapping->host;
486
487 /* Support only block device in this moment */
488 if (!S_ISBLK(inode->i_mode)) {
489 err = -ENOTBLK;
490 goto out;
491 }
492
493 bdev = blkdev_get_by_dev(inode->i_rdev,
494 FMODE_READ | FMODE_WRITE | FMODE_EXCL, zram);
495 if (IS_ERR(bdev)) {
496 err = PTR_ERR(bdev);
497 bdev = NULL;
498 goto out;
499 }
500
501 nr_pages = i_size_read(inode) >> PAGE_SHIFT;
502 bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
503 bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
504 if (!bitmap) {
505 err = -ENOMEM;
506 goto out;
507 }
508
509 reset_bdev(zram);
510
511 zram->bdev = bdev;
512 zram->backing_dev = backing_dev;
513 zram->bitmap = bitmap;
514 zram->nr_pages = nr_pages;
515 /*
516 * With writeback feature, zram does asynchronous IO so it's no longer
517 * synchronous device so let's remove synchronous io flag. Othewise,
518 * upper layer(e.g., swap) could wait IO completion rather than
519 * (submit and return), which will cause system sluggish.
520 * Furthermore, when the IO function returns(e.g., swap_readpage),
521 * upper layer expects IO was done so it could deallocate the page
522 * freely but in fact, IO is going on so finally could cause
523 * use-after-free when the IO is really done.
524 */
525 zram->disk->fops = &zram_wb_devops;
526 up_write(&zram->init_lock);
527
528 pr_info("setup backing device %s\n", file_name);
529 kfree(file_name);
530
531 return len;
532 out:
533 if (bitmap)
534 kvfree(bitmap);
535
536 if (bdev)
537 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
538
539 if (backing_dev)
540 filp_close(backing_dev, NULL);
541
542 up_write(&zram->init_lock);
543
544 kfree(file_name);
545
546 return err;
547 }
548
549 static unsigned long alloc_block_bdev(struct zram *zram)
550 {
551 unsigned long blk_idx = 1;
552 retry:
553 /* skip 0 bit to confuse zram.handle = 0 */
554 blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
555 if (blk_idx == zram->nr_pages)
556 return 0;
557
558 if (test_and_set_bit(blk_idx, zram->bitmap))
559 goto retry;
560
561 atomic64_inc(&zram->stats.bd_count);
562 return blk_idx;
563 }
564
565 static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
566 {
567 int was_set;
568
569 was_set = test_and_clear_bit(blk_idx, zram->bitmap);
570 WARN_ON_ONCE(!was_set);
571 atomic64_dec(&zram->stats.bd_count);
572 }
573
574 static void zram_page_end_io(struct bio *bio)
575 {
576 struct page *page = bio_first_page_all(bio);
577
578 page_endio(page, op_is_write(bio_op(bio)),
579 blk_status_to_errno(bio->bi_status));
580 bio_put(bio);
581 }
582
583 /*
584 * Returns 1 if the submission is successful.
585 */
586 static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec,
587 unsigned long entry, struct bio *parent)
588 {
589 struct bio *bio;
590
591 bio = bio_alloc(GFP_ATOMIC, 1);
592 if (!bio)
593 return -ENOMEM;
594
595 bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
596 bio_set_dev(bio, zram->bdev);
597 if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) {
598 bio_put(bio);
599 return -EIO;
600 }
601
602 if (!parent) {
603 bio->bi_opf = REQ_OP_READ;
604 bio->bi_end_io = zram_page_end_io;
605 } else {
606 bio->bi_opf = parent->bi_opf;
607 bio_chain(bio, parent);
608 }
609
610 submit_bio(bio);
611 return 1;
612 }
613
614 #define PAGE_WB_SIG "page_index="
615
616 #define PAGE_WRITEBACK 0
617 #define HUGE_WRITEBACK 1
618 #define IDLE_WRITEBACK 2
619
620
621 static ssize_t writeback_store(struct device *dev,
622 struct device_attribute *attr, const char *buf, size_t len)
623 {
624 struct zram *zram = dev_to_zram(dev);
625 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
626 unsigned long index = 0;
627 struct bio bio;
628 struct bio_vec bio_vec;
629 struct page *page;
630 ssize_t ret = len;
631 int mode, err;
632 unsigned long blk_idx = 0;
633
634 if (sysfs_streq(buf, "idle"))
635 mode = IDLE_WRITEBACK;
636 else if (sysfs_streq(buf, "huge"))
637 mode = HUGE_WRITEBACK;
638 else {
639 if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1))
640 return -EINVAL;
641
642 ret = kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index);
643 if (ret || index >= nr_pages)
644 return -EINVAL;
645
646 nr_pages = 1;
647 mode = PAGE_WRITEBACK;
648 }
649
650 down_read(&zram->init_lock);
651 if (!init_done(zram)) {
652 ret = -EINVAL;
653 goto release_init_lock;
654 }
655
656 if (!zram->backing_dev) {
657 ret = -ENODEV;
658 goto release_init_lock;
659 }
660
661 page = alloc_page(GFP_KERNEL);
662 if (!page) {
663 ret = -ENOMEM;
664 goto release_init_lock;
665 }
666
667 while (nr_pages--) {
668 struct bio_vec bvec;
669
670 bvec.bv_page = page;
671 bvec.bv_len = PAGE_SIZE;
672 bvec.bv_offset = 0;
673
674 spin_lock(&zram->wb_limit_lock);
675 if (zram->wb_limit_enable && !zram->bd_wb_limit) {
676 spin_unlock(&zram->wb_limit_lock);
677 ret = -EIO;
678 break;
679 }
680 spin_unlock(&zram->wb_limit_lock);
681
682 if (!blk_idx) {
683 blk_idx = alloc_block_bdev(zram);
684 if (!blk_idx) {
685 ret = -ENOSPC;
686 break;
687 }
688 }
689
690 zram_slot_lock(zram, index);
691 if (!zram_allocated(zram, index))
692 goto next;
693
694 if (zram_test_flag(zram, index, ZRAM_WB) ||
695 zram_test_flag(zram, index, ZRAM_SAME) ||
696 zram_test_flag(zram, index, ZRAM_UNDER_WB))
697 goto next;
698
699 if (mode == IDLE_WRITEBACK &&
700 !zram_test_flag(zram, index, ZRAM_IDLE))
701 goto next;
702 if (mode == HUGE_WRITEBACK &&
703 !zram_test_flag(zram, index, ZRAM_HUGE))
704 goto next;
705 /*
706 * Clearing ZRAM_UNDER_WB is duty of caller.
707 * IOW, zram_free_page never clear it.
708 */
709 zram_set_flag(zram, index, ZRAM_UNDER_WB);
710 /* Need for hugepage writeback racing */
711 zram_set_flag(zram, index, ZRAM_IDLE);
712 zram_slot_unlock(zram, index);
713 if (zram_bvec_read(zram, &bvec, index, 0, NULL)) {
714 zram_slot_lock(zram, index);
715 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
716 zram_clear_flag(zram, index, ZRAM_IDLE);
717 zram_slot_unlock(zram, index);
718 continue;
719 }
720
721 bio_init(&bio, &bio_vec, 1);
722 bio_set_dev(&bio, zram->bdev);
723 bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
724 bio.bi_opf = REQ_OP_WRITE | REQ_SYNC;
725
726 bio_add_page(&bio, bvec.bv_page, bvec.bv_len,
727 bvec.bv_offset);
728 /*
729 * XXX: A single page IO would be inefficient for write
730 * but it would be not bad as starter.
731 */
732 err = submit_bio_wait(&bio);
733 if (err) {
734 zram_slot_lock(zram, index);
735 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
736 zram_clear_flag(zram, index, ZRAM_IDLE);
737 zram_slot_unlock(zram, index);
738 /*
739 * Return last IO error unless every IO were
740 * not suceeded.
741 */
742 ret = err;
743 continue;
744 }
745
746 atomic64_inc(&zram->stats.bd_writes);
747 /*
748 * We released zram_slot_lock so need to check if the slot was
749 * changed. If there is freeing for the slot, we can catch it
750 * easily by zram_allocated.
751 * A subtle case is the slot is freed/reallocated/marked as
752 * ZRAM_IDLE again. To close the race, idle_store doesn't
753 * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
754 * Thus, we could close the race by checking ZRAM_IDLE bit.
755 */
756 zram_slot_lock(zram, index);
757 if (!zram_allocated(zram, index) ||
758 !zram_test_flag(zram, index, ZRAM_IDLE)) {
759 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
760 zram_clear_flag(zram, index, ZRAM_IDLE);
761 goto next;
762 }
763
764 zram_free_page(zram, index);
765 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
766 zram_set_flag(zram, index, ZRAM_WB);
767 zram_set_element(zram, index, blk_idx);
768 blk_idx = 0;
769 atomic64_inc(&zram->stats.pages_stored);
770 spin_lock(&zram->wb_limit_lock);
771 if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
772 zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
773 spin_unlock(&zram->wb_limit_lock);
774 next:
775 zram_slot_unlock(zram, index);
776 }
777
778 if (blk_idx)
779 free_block_bdev(zram, blk_idx);
780 __free_page(page);
781 release_init_lock:
782 up_read(&zram->init_lock);
783
784 return ret;
785 }
786
787 struct zram_work {
788 struct work_struct work;
789 struct zram *zram;
790 unsigned long entry;
791 struct bio *bio;
792 struct bio_vec bvec;
793 };
794
795 #if PAGE_SIZE != 4096
796 static void zram_sync_read(struct work_struct *work)
797 {
798 struct zram_work *zw = container_of(work, struct zram_work, work);
799 struct zram *zram = zw->zram;
800 unsigned long entry = zw->entry;
801 struct bio *bio = zw->bio;
802
803 read_from_bdev_async(zram, &zw->bvec, entry, bio);
804 }
805
806 /*
807 * Block layer want one ->submit_bio to be active at a time, so if we use
808 * chained IO with parent IO in same context, it's a deadlock. To avoid that,
809 * use a worker thread context.
810 */
811 static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
812 unsigned long entry, struct bio *bio)
813 {
814 struct zram_work work;
815
816 work.bvec = *bvec;
817 work.zram = zram;
818 work.entry = entry;
819 work.bio = bio;
820
821 INIT_WORK_ONSTACK(&work.work, zram_sync_read);
822 queue_work(system_unbound_wq, &work.work);
823 flush_work(&work.work);
824 destroy_work_on_stack(&work.work);
825
826 return 1;
827 }
828 #else
829 static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
830 unsigned long entry, struct bio *bio)
831 {
832 WARN_ON(1);
833 return -EIO;
834 }
835 #endif
836
837 static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
838 unsigned long entry, struct bio *parent, bool sync)
839 {
840 atomic64_inc(&zram->stats.bd_reads);
841 if (sync)
842 return read_from_bdev_sync(zram, bvec, entry, parent);
843 else
844 return read_from_bdev_async(zram, bvec, entry, parent);
845 }
846 #else
847 static inline void reset_bdev(struct zram *zram) {};
848 static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
849 unsigned long entry, struct bio *parent, bool sync)
850 {
851 return -EIO;
852 }
853
854 static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
855 #endif
856
857 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
858
859 static struct dentry *zram_debugfs_root;
860
861 static void zram_debugfs_create(void)
862 {
863 zram_debugfs_root = debugfs_create_dir("zram", NULL);
864 }
865
866 static void zram_debugfs_destroy(void)
867 {
868 debugfs_remove_recursive(zram_debugfs_root);
869 }
870
871 static void zram_accessed(struct zram *zram, u32 index)
872 {
873 zram_clear_flag(zram, index, ZRAM_IDLE);
874 zram->table[index].ac_time = ktime_get_boottime();
875 }
876
877 static ssize_t read_block_state(struct file *file, char __user *buf,
878 size_t count, loff_t *ppos)
879 {
880 char *kbuf;
881 ssize_t index, written = 0;
882 struct zram *zram = file->private_data;
883 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
884 struct timespec64 ts;
885
886 kbuf = kvmalloc(count, GFP_KERNEL);
887 if (!kbuf)
888 return -ENOMEM;
889
890 down_read(&zram->init_lock);
891 if (!init_done(zram)) {
892 up_read(&zram->init_lock);
893 kvfree(kbuf);
894 return -EINVAL;
895 }
896
897 for (index = *ppos; index < nr_pages; index++) {
898 int copied;
899
900 zram_slot_lock(zram, index);
901 if (!zram_allocated(zram, index))
902 goto next;
903
904 ts = ktime_to_timespec64(zram->table[index].ac_time);
905 copied = snprintf(kbuf + written, count,
906 "%12zd %12lld.%06lu %c%c%c%c\n",
907 index, (s64)ts.tv_sec,
908 ts.tv_nsec / NSEC_PER_USEC,
909 zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
910 zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
911 zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
912 zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.');
913
914 if (count < copied) {
915 zram_slot_unlock(zram, index);
916 break;
917 }
918 written += copied;
919 count -= copied;
920 next:
921 zram_slot_unlock(zram, index);
922 *ppos += 1;
923 }
924
925 up_read(&zram->init_lock);
926 if (copy_to_user(buf, kbuf, written))
927 written = -EFAULT;
928 kvfree(kbuf);
929
930 return written;
931 }
932
933 static const struct file_operations proc_zram_block_state_op = {
934 .open = simple_open,
935 .read = read_block_state,
936 .llseek = default_llseek,
937 };
938
939 static void zram_debugfs_register(struct zram *zram)
940 {
941 if (!zram_debugfs_root)
942 return;
943
944 zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
945 zram_debugfs_root);
946 debugfs_create_file("block_state", 0400, zram->debugfs_dir,
947 zram, &proc_zram_block_state_op);
948 }
949
950 static void zram_debugfs_unregister(struct zram *zram)
951 {
952 debugfs_remove_recursive(zram->debugfs_dir);
953 }
954 #else
955 static void zram_debugfs_create(void) {};
956 static void zram_debugfs_destroy(void) {};
957 static void zram_accessed(struct zram *zram, u32 index)
958 {
959 zram_clear_flag(zram, index, ZRAM_IDLE);
960 };
961 static void zram_debugfs_register(struct zram *zram) {};
962 static void zram_debugfs_unregister(struct zram *zram) {};
963 #endif
964
965 /*
966 * We switched to per-cpu streams and this attr is not needed anymore.
967 * However, we will keep it around for some time, because:
968 * a) we may revert per-cpu streams in the future
969 * b) it's visible to user space and we need to follow our 2 years
970 * retirement rule; but we already have a number of 'soon to be
971 * altered' attrs, so max_comp_streams need to wait for the next
972 * layoff cycle.
973 */
974 static ssize_t max_comp_streams_show(struct device *dev,
975 struct device_attribute *attr, char *buf)
976 {
977 return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
978 }
979
980 static ssize_t max_comp_streams_store(struct device *dev,
981 struct device_attribute *attr, const char *buf, size_t len)
982 {
983 return len;
984 }
985
986 static ssize_t comp_algorithm_show(struct device *dev,
987 struct device_attribute *attr, char *buf)
988 {
989 size_t sz;
990 struct zram *zram = dev_to_zram(dev);
991
992 down_read(&zram->init_lock);
993 sz = zcomp_available_show(zram->compressor, buf);
994 up_read(&zram->init_lock);
995
996 return sz;
997 }
998
999 static ssize_t comp_algorithm_store(struct device *dev,
1000 struct device_attribute *attr, const char *buf, size_t len)
1001 {
1002 struct zram *zram = dev_to_zram(dev);
1003 char compressor[ARRAY_SIZE(zram->compressor)];
1004 size_t sz;
1005
1006 strlcpy(compressor, buf, sizeof(compressor));
1007 /* ignore trailing newline */
1008 sz = strlen(compressor);
1009 if (sz > 0 && compressor[sz - 1] == '\n')
1010 compressor[sz - 1] = 0x00;
1011
1012 if (!zcomp_available_algorithm(compressor))
1013 return -EINVAL;
1014
1015 down_write(&zram->init_lock);
1016 if (init_done(zram)) {
1017 up_write(&zram->init_lock);
1018 pr_info("Can't change algorithm for initialized device\n");
1019 return -EBUSY;
1020 }
1021
1022 strcpy(zram->compressor, compressor);
1023 up_write(&zram->init_lock);
1024 return len;
1025 }
1026
1027 static ssize_t compact_store(struct device *dev,
1028 struct device_attribute *attr, const char *buf, size_t len)
1029 {
1030 struct zram *zram = dev_to_zram(dev);
1031
1032 down_read(&zram->init_lock);
1033 if (!init_done(zram)) {
1034 up_read(&zram->init_lock);
1035 return -EINVAL;
1036 }
1037
1038 zs_compact(zram->mem_pool);
1039 up_read(&zram->init_lock);
1040
1041 return len;
1042 }
1043
1044 static ssize_t io_stat_show(struct device *dev,
1045 struct device_attribute *attr, char *buf)
1046 {
1047 struct zram *zram = dev_to_zram(dev);
1048 ssize_t ret;
1049
1050 down_read(&zram->init_lock);
1051 ret = scnprintf(buf, PAGE_SIZE,
1052 "%8llu %8llu %8llu %8llu\n",
1053 (u64)atomic64_read(&zram->stats.failed_reads),
1054 (u64)atomic64_read(&zram->stats.failed_writes),
1055 (u64)atomic64_read(&zram->stats.invalid_io),
1056 (u64)atomic64_read(&zram->stats.notify_free));
1057 up_read(&zram->init_lock);
1058
1059 return ret;
1060 }
1061
1062 static ssize_t mm_stat_show(struct device *dev,
1063 struct device_attribute *attr, char *buf)
1064 {
1065 struct zram *zram = dev_to_zram(dev);
1066 struct zs_pool_stats pool_stats;
1067 u64 orig_size, mem_used = 0;
1068 long max_used;
1069 ssize_t ret;
1070
1071 memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
1072
1073 down_read(&zram->init_lock);
1074 if (init_done(zram)) {
1075 mem_used = zs_get_total_pages(zram->mem_pool);
1076 zs_pool_stats(zram->mem_pool, &pool_stats);
1077 }
1078
1079 orig_size = atomic64_read(&zram->stats.pages_stored);
1080 max_used = atomic_long_read(&zram->stats.max_used_pages);
1081
1082 ret = scnprintf(buf, PAGE_SIZE,
1083 "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
1084 orig_size << PAGE_SHIFT,
1085 (u64)atomic64_read(&zram->stats.compr_data_size),
1086 mem_used << PAGE_SHIFT,
1087 zram->limit_pages << PAGE_SHIFT,
1088 max_used << PAGE_SHIFT,
1089 (u64)atomic64_read(&zram->stats.same_pages),
1090 atomic_long_read(&pool_stats.pages_compacted),
1091 (u64)atomic64_read(&zram->stats.huge_pages),
1092 (u64)atomic64_read(&zram->stats.huge_pages_since));
1093 up_read(&zram->init_lock);
1094
1095 return ret;
1096 }
1097
1098 #ifdef CONFIG_ZRAM_WRITEBACK
1099 #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
1100 static ssize_t bd_stat_show(struct device *dev,
1101 struct device_attribute *attr, char *buf)
1102 {
1103 struct zram *zram = dev_to_zram(dev);
1104 ssize_t ret;
1105
1106 down_read(&zram->init_lock);
1107 ret = scnprintf(buf, PAGE_SIZE,
1108 "%8llu %8llu %8llu\n",
1109 FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
1110 FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
1111 FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
1112 up_read(&zram->init_lock);
1113
1114 return ret;
1115 }
1116 #endif
1117
1118 static ssize_t debug_stat_show(struct device *dev,
1119 struct device_attribute *attr, char *buf)
1120 {
1121 int version = 1;
1122 struct zram *zram = dev_to_zram(dev);
1123 ssize_t ret;
1124
1125 down_read(&zram->init_lock);
1126 ret = scnprintf(buf, PAGE_SIZE,
1127 "version: %d\n%8llu %8llu\n",
1128 version,
1129 (u64)atomic64_read(&zram->stats.writestall),
1130 (u64)atomic64_read(&zram->stats.miss_free));
1131 up_read(&zram->init_lock);
1132
1133 return ret;
1134 }
1135
1136 static DEVICE_ATTR_RO(io_stat);
1137 static DEVICE_ATTR_RO(mm_stat);
1138 #ifdef CONFIG_ZRAM_WRITEBACK
1139 static DEVICE_ATTR_RO(bd_stat);
1140 #endif
1141 static DEVICE_ATTR_RO(debug_stat);
1142
1143 static void zram_meta_free(struct zram *zram, u64 disksize)
1144 {
1145 size_t num_pages = disksize >> PAGE_SHIFT;
1146 size_t index;
1147
1148 /* Free all pages that are still in this zram device */
1149 for (index = 0; index < num_pages; index++)
1150 zram_free_page(zram, index);
1151
1152 zs_destroy_pool(zram->mem_pool);
1153 vfree(zram->table);
1154 }
1155
1156 static bool zram_meta_alloc(struct zram *zram, u64 disksize)
1157 {
1158 size_t num_pages;
1159
1160 num_pages = disksize >> PAGE_SHIFT;
1161 zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
1162 if (!zram->table)
1163 return false;
1164
1165 zram->mem_pool = zs_create_pool(zram->disk->disk_name);
1166 if (!zram->mem_pool) {
1167 vfree(zram->table);
1168 return false;
1169 }
1170
1171 if (!huge_class_size)
1172 huge_class_size = zs_huge_class_size(zram->mem_pool);
1173 return true;
1174 }
1175
1176 /*
1177 * To protect concurrent access to the same index entry,
1178 * caller should hold this table index entry's bit_spinlock to
1179 * indicate this index entry is accessing.
1180 */
1181 static void zram_free_page(struct zram *zram, size_t index)
1182 {
1183 unsigned long handle;
1184
1185 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
1186 zram->table[index].ac_time = 0;
1187 #endif
1188 if (zram_test_flag(zram, index, ZRAM_IDLE))
1189 zram_clear_flag(zram, index, ZRAM_IDLE);
1190
1191 if (zram_test_flag(zram, index, ZRAM_HUGE)) {
1192 zram_clear_flag(zram, index, ZRAM_HUGE);
1193 atomic64_dec(&zram->stats.huge_pages);
1194 }
1195
1196 if (zram_test_flag(zram, index, ZRAM_WB)) {
1197 zram_clear_flag(zram, index, ZRAM_WB);
1198 free_block_bdev(zram, zram_get_element(zram, index));
1199 goto out;
1200 }
1201
1202 /*
1203 * No memory is allocated for same element filled pages.
1204 * Simply clear same page flag.
1205 */
1206 if (zram_test_flag(zram, index, ZRAM_SAME)) {
1207 zram_clear_flag(zram, index, ZRAM_SAME);
1208 atomic64_dec(&zram->stats.same_pages);
1209 goto out;
1210 }
1211
1212 handle = zram_get_handle(zram, index);
1213 if (!handle)
1214 return;
1215
1216 zs_free(zram->mem_pool, handle);
1217
1218 atomic64_sub(zram_get_obj_size(zram, index),
1219 &zram->stats.compr_data_size);
1220 out:
1221 atomic64_dec(&zram->stats.pages_stored);
1222 zram_set_handle(zram, index, 0);
1223 zram_set_obj_size(zram, index, 0);
1224 WARN_ON_ONCE(zram->table[index].flags &
1225 ~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB));
1226 }
1227
1228 static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
1229 struct bio *bio, bool partial_io)
1230 {
1231 struct zcomp_strm *zstrm;
1232 unsigned long handle;
1233 unsigned int size;
1234 void *src, *dst;
1235 int ret;
1236
1237 zram_slot_lock(zram, index);
1238 if (zram_test_flag(zram, index, ZRAM_WB)) {
1239 struct bio_vec bvec;
1240
1241 zram_slot_unlock(zram, index);
1242
1243 bvec.bv_page = page;
1244 bvec.bv_len = PAGE_SIZE;
1245 bvec.bv_offset = 0;
1246 return read_from_bdev(zram, &bvec,
1247 zram_get_element(zram, index),
1248 bio, partial_io);
1249 }
1250
1251 handle = zram_get_handle(zram, index);
1252 if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
1253 unsigned long value;
1254 void *mem;
1255
1256 value = handle ? zram_get_element(zram, index) : 0;
1257 mem = kmap_atomic(page);
1258 zram_fill_page(mem, PAGE_SIZE, value);
1259 kunmap_atomic(mem);
1260 zram_slot_unlock(zram, index);
1261 return 0;
1262 }
1263
1264 size = zram_get_obj_size(zram, index);
1265
1266 if (size != PAGE_SIZE)
1267 zstrm = zcomp_stream_get(zram->comp);
1268
1269 src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
1270 if (size == PAGE_SIZE) {
1271 dst = kmap_atomic(page);
1272 memcpy(dst, src, PAGE_SIZE);
1273 kunmap_atomic(dst);
1274 ret = 0;
1275 } else {
1276 dst = kmap_atomic(page);
1277 ret = zcomp_decompress(zstrm, src, size, dst);
1278 kunmap_atomic(dst);
1279 zcomp_stream_put(zram->comp);
1280 }
1281 zs_unmap_object(zram->mem_pool, handle);
1282 zram_slot_unlock(zram, index);
1283
1284 /* Should NEVER happen. Return bio error if it does. */
1285 if (WARN_ON(ret))
1286 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
1287
1288 return ret;
1289 }
1290
1291 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
1292 u32 index, int offset, struct bio *bio)
1293 {
1294 int ret;
1295 struct page *page;
1296
1297 page = bvec->bv_page;
1298 if (is_partial_io(bvec)) {
1299 /* Use a temporary buffer to decompress the page */
1300 page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
1301 if (!page)
1302 return -ENOMEM;
1303 }
1304
1305 ret = __zram_bvec_read(zram, page, index, bio, is_partial_io(bvec));
1306 if (unlikely(ret))
1307 goto out;
1308
1309 if (is_partial_io(bvec)) {
1310 void *dst = kmap_atomic(bvec->bv_page);
1311 void *src = kmap_atomic(page);
1312
1313 memcpy(dst + bvec->bv_offset, src + offset, bvec->bv_len);
1314 kunmap_atomic(src);
1315 kunmap_atomic(dst);
1316 }
1317 out:
1318 if (is_partial_io(bvec))
1319 __free_page(page);
1320
1321 return ret;
1322 }
1323
1324 static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1325 u32 index, struct bio *bio)
1326 {
1327 int ret = 0;
1328 unsigned long alloced_pages;
1329 unsigned long handle = 0;
1330 unsigned int comp_len = 0;
1331 void *src, *dst, *mem;
1332 struct zcomp_strm *zstrm;
1333 struct page *page = bvec->bv_page;
1334 unsigned long element = 0;
1335 enum zram_pageflags flags = 0;
1336
1337 mem = kmap_atomic(page);
1338 if (page_same_filled(mem, &element)) {
1339 kunmap_atomic(mem);
1340 /* Free memory associated with this sector now. */
1341 flags = ZRAM_SAME;
1342 atomic64_inc(&zram->stats.same_pages);
1343 goto out;
1344 }
1345 kunmap_atomic(mem);
1346
1347 compress_again:
1348 zstrm = zcomp_stream_get(zram->comp);
1349 src = kmap_atomic(page);
1350 ret = zcomp_compress(zstrm, src, &comp_len);
1351 kunmap_atomic(src);
1352
1353 if (unlikely(ret)) {
1354 zcomp_stream_put(zram->comp);
1355 pr_err("Compression failed! err=%d\n", ret);
1356 zs_free(zram->mem_pool, handle);
1357 return ret;
1358 }
1359
1360 if (comp_len >= huge_class_size)
1361 comp_len = PAGE_SIZE;
1362 /*
1363 * handle allocation has 2 paths:
1364 * a) fast path is executed with preemption disabled (for
1365 * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear,
1366 * since we can't sleep;
1367 * b) slow path enables preemption and attempts to allocate
1368 * the page with __GFP_DIRECT_RECLAIM bit set. we have to
1369 * put per-cpu compression stream and, thus, to re-do
1370 * the compression once handle is allocated.
1371 *
1372 * if we have a 'non-null' handle here then we are coming
1373 * from the slow path and handle has already been allocated.
1374 */
1375 if (!handle)
1376 handle = zs_malloc(zram->mem_pool, comp_len,
1377 __GFP_KSWAPD_RECLAIM |
1378 __GFP_NOWARN |
1379 __GFP_HIGHMEM |
1380 __GFP_MOVABLE);
1381 if (!handle) {
1382 zcomp_stream_put(zram->comp);
1383 atomic64_inc(&zram->stats.writestall);
1384 handle = zs_malloc(zram->mem_pool, comp_len,
1385 GFP_NOIO | __GFP_HIGHMEM |
1386 __GFP_MOVABLE);
1387 if (handle)
1388 goto compress_again;
1389 return -ENOMEM;
1390 }
1391
1392 alloced_pages = zs_get_total_pages(zram->mem_pool);
1393 update_used_max(zram, alloced_pages);
1394
1395 if (zram->limit_pages && alloced_pages > zram->limit_pages) {
1396 zcomp_stream_put(zram->comp);
1397 zs_free(zram->mem_pool, handle);
1398 return -ENOMEM;
1399 }
1400
1401 dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
1402
1403 src = zstrm->buffer;
1404 if (comp_len == PAGE_SIZE)
1405 src = kmap_atomic(page);
1406 memcpy(dst, src, comp_len);
1407 if (comp_len == PAGE_SIZE)
1408 kunmap_atomic(src);
1409
1410 zcomp_stream_put(zram->comp);
1411 zs_unmap_object(zram->mem_pool, handle);
1412 atomic64_add(comp_len, &zram->stats.compr_data_size);
1413 out:
1414 /*
1415 * Free memory associated with this sector
1416 * before overwriting unused sectors.
1417 */
1418 zram_slot_lock(zram, index);
1419 zram_free_page(zram, index);
1420
1421 if (comp_len == PAGE_SIZE) {
1422 zram_set_flag(zram, index, ZRAM_HUGE);
1423 atomic64_inc(&zram->stats.huge_pages);
1424 atomic64_inc(&zram->stats.huge_pages_since);
1425 }
1426
1427 if (flags) {
1428 zram_set_flag(zram, index, flags);
1429 zram_set_element(zram, index, element);
1430 } else {
1431 zram_set_handle(zram, index, handle);
1432 zram_set_obj_size(zram, index, comp_len);
1433 }
1434 zram_slot_unlock(zram, index);
1435
1436 /* Update stats */
1437 atomic64_inc(&zram->stats.pages_stored);
1438 return ret;
1439 }
1440
1441 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1442 u32 index, int offset, struct bio *bio)
1443 {
1444 int ret;
1445 struct page *page = NULL;
1446 void *src;
1447 struct bio_vec vec;
1448
1449 vec = *bvec;
1450 if (is_partial_io(bvec)) {
1451 void *dst;
1452 /*
1453 * This is a partial IO. We need to read the full page
1454 * before to write the changes.
1455 */
1456 page = alloc_page(GFP_NOIO|__GFP_HIGHMEM);
1457 if (!page)
1458 return -ENOMEM;
1459
1460 ret = __zram_bvec_read(zram, page, index, bio, true);
1461 if (ret)
1462 goto out;
1463
1464 src = kmap_atomic(bvec->bv_page);
1465 dst = kmap_atomic(page);
1466 memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len);
1467 kunmap_atomic(dst);
1468 kunmap_atomic(src);
1469
1470 vec.bv_page = page;
1471 vec.bv_len = PAGE_SIZE;
1472 vec.bv_offset = 0;
1473 }
1474
1475 ret = __zram_bvec_write(zram, &vec, index, bio);
1476 out:
1477 if (is_partial_io(bvec))
1478 __free_page(page);
1479 return ret;
1480 }
1481
1482 /*
1483 * zram_bio_discard - handler on discard request
1484 * @index: physical block index in PAGE_SIZE units
1485 * @offset: byte offset within physical block
1486 */
1487 static void zram_bio_discard(struct zram *zram, u32 index,
1488 int offset, struct bio *bio)
1489 {
1490 size_t n = bio->bi_iter.bi_size;
1491
1492 /*
1493 * zram manages data in physical block size units. Because logical block
1494 * size isn't identical with physical block size on some arch, we
1495 * could get a discard request pointing to a specific offset within a
1496 * certain physical block. Although we can handle this request by
1497 * reading that physiclal block and decompressing and partially zeroing
1498 * and re-compressing and then re-storing it, this isn't reasonable
1499 * because our intent with a discard request is to save memory. So
1500 * skipping this logical block is appropriate here.
1501 */
1502 if (offset) {
1503 if (n <= (PAGE_SIZE - offset))
1504 return;
1505
1506 n -= (PAGE_SIZE - offset);
1507 index++;
1508 }
1509
1510 while (n >= PAGE_SIZE) {
1511 zram_slot_lock(zram, index);
1512 zram_free_page(zram, index);
1513 zram_slot_unlock(zram, index);
1514 atomic64_inc(&zram->stats.notify_free);
1515 index++;
1516 n -= PAGE_SIZE;
1517 }
1518 }
1519
1520 /*
1521 * Returns errno if it has some problem. Otherwise return 0 or 1.
1522 * Returns 0 if IO request was done synchronously
1523 * Returns 1 if IO request was successfully submitted.
1524 */
1525 static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
1526 int offset, unsigned int op, struct bio *bio)
1527 {
1528 int ret;
1529
1530 if (!op_is_write(op)) {
1531 atomic64_inc(&zram->stats.num_reads);
1532 ret = zram_bvec_read(zram, bvec, index, offset, bio);
1533 flush_dcache_page(bvec->bv_page);
1534 } else {
1535 atomic64_inc(&zram->stats.num_writes);
1536 ret = zram_bvec_write(zram, bvec, index, offset, bio);
1537 }
1538
1539 zram_slot_lock(zram, index);
1540 zram_accessed(zram, index);
1541 zram_slot_unlock(zram, index);
1542
1543 if (unlikely(ret < 0)) {
1544 if (!op_is_write(op))
1545 atomic64_inc(&zram->stats.failed_reads);
1546 else
1547 atomic64_inc(&zram->stats.failed_writes);
1548 }
1549
1550 return ret;
1551 }
1552
1553 static void __zram_make_request(struct zram *zram, struct bio *bio)
1554 {
1555 int offset;
1556 u32 index;
1557 struct bio_vec bvec;
1558 struct bvec_iter iter;
1559 unsigned long start_time;
1560
1561 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1562 offset = (bio->bi_iter.bi_sector &
1563 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
1564
1565 switch (bio_op(bio)) {
1566 case REQ_OP_DISCARD:
1567 case REQ_OP_WRITE_ZEROES:
1568 zram_bio_discard(zram, index, offset, bio);
1569 bio_endio(bio);
1570 return;
1571 default:
1572 break;
1573 }
1574
1575 start_time = bio_start_io_acct(bio);
1576 bio_for_each_segment(bvec, bio, iter) {
1577 struct bio_vec bv = bvec;
1578 unsigned int unwritten = bvec.bv_len;
1579
1580 do {
1581 bv.bv_len = min_t(unsigned int, PAGE_SIZE - offset,
1582 unwritten);
1583 if (zram_bvec_rw(zram, &bv, index, offset,
1584 bio_op(bio), bio) < 0) {
1585 bio->bi_status = BLK_STS_IOERR;
1586 break;
1587 }
1588
1589 bv.bv_offset += bv.bv_len;
1590 unwritten -= bv.bv_len;
1591
1592 update_position(&index, &offset, &bv);
1593 } while (unwritten);
1594 }
1595 bio_end_io_acct(bio, start_time);
1596 bio_endio(bio);
1597 }
1598
1599 /*
1600 * Handler function for all zram I/O requests.
1601 */
1602 static blk_qc_t zram_submit_bio(struct bio *bio)
1603 {
1604 struct zram *zram = bio->bi_disk->private_data;
1605
1606 if (!valid_io_request(zram, bio->bi_iter.bi_sector,
1607 bio->bi_iter.bi_size)) {
1608 atomic64_inc(&zram->stats.invalid_io);
1609 goto error;
1610 }
1611
1612 __zram_make_request(zram, bio);
1613 return BLK_QC_T_NONE;
1614
1615 error:
1616 bio_io_error(bio);
1617 return BLK_QC_T_NONE;
1618 }
1619
1620 static void zram_slot_free_notify(struct block_device *bdev,
1621 unsigned long index)
1622 {
1623 struct zram *zram;
1624
1625 zram = bdev->bd_disk->private_data;
1626
1627 atomic64_inc(&zram->stats.notify_free);
1628 if (!zram_slot_trylock(zram, index)) {
1629 atomic64_inc(&zram->stats.miss_free);
1630 return;
1631 }
1632
1633 zram_free_page(zram, index);
1634 zram_slot_unlock(zram, index);
1635 }
1636
1637 static int zram_rw_page(struct block_device *bdev, sector_t sector,
1638 struct page *page, unsigned int op)
1639 {
1640 int offset, ret;
1641 u32 index;
1642 struct zram *zram;
1643 struct bio_vec bv;
1644 unsigned long start_time;
1645
1646 if (PageTransHuge(page))
1647 return -ENOTSUPP;
1648 zram = bdev->bd_disk->private_data;
1649
1650 if (!valid_io_request(zram, sector, PAGE_SIZE)) {
1651 atomic64_inc(&zram->stats.invalid_io);
1652 ret = -EINVAL;
1653 goto out;
1654 }
1655
1656 index = sector >> SECTORS_PER_PAGE_SHIFT;
1657 offset = (sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
1658
1659 bv.bv_page = page;
1660 bv.bv_len = PAGE_SIZE;
1661 bv.bv_offset = 0;
1662
1663 start_time = disk_start_io_acct(bdev->bd_disk, SECTORS_PER_PAGE, op);
1664 ret = zram_bvec_rw(zram, &bv, index, offset, op, NULL);
1665 disk_end_io_acct(bdev->bd_disk, op, start_time);
1666 out:
1667 /*
1668 * If I/O fails, just return error(ie, non-zero) without
1669 * calling page_endio.
1670 * It causes resubmit the I/O with bio request by upper functions
1671 * of rw_page(e.g., swap_readpage, __swap_writepage) and
1672 * bio->bi_end_io does things to handle the error
1673 * (e.g., SetPageError, set_page_dirty and extra works).
1674 */
1675 if (unlikely(ret < 0))
1676 return ret;
1677
1678 switch (ret) {
1679 case 0:
1680 page_endio(page, op_is_write(op), 0);
1681 break;
1682 case 1:
1683 ret = 0;
1684 break;
1685 default:
1686 WARN_ON(1);
1687 }
1688 return ret;
1689 }
1690
1691 static void zram_reset_device(struct zram *zram)
1692 {
1693 struct zcomp *comp;
1694 u64 disksize;
1695
1696 down_write(&zram->init_lock);
1697
1698 zram->limit_pages = 0;
1699
1700 if (!init_done(zram)) {
1701 up_write(&zram->init_lock);
1702 return;
1703 }
1704
1705 comp = zram->comp;
1706 disksize = zram->disksize;
1707 zram->disksize = 0;
1708
1709 set_capacity_and_notify(zram->disk, 0);
1710 part_stat_set_all(zram->disk->part0, 0);
1711
1712 up_write(&zram->init_lock);
1713 /* I/O operation under all of CPU are done so let's free */
1714 zram_meta_free(zram, disksize);
1715 memset(&zram->stats, 0, sizeof(zram->stats));
1716 zcomp_destroy(comp);
1717 reset_bdev(zram);
1718 }
1719
1720 static ssize_t disksize_store(struct device *dev,
1721 struct device_attribute *attr, const char *buf, size_t len)
1722 {
1723 u64 disksize;
1724 struct zcomp *comp;
1725 struct zram *zram = dev_to_zram(dev);
1726 int err;
1727
1728 disksize = memparse(buf, NULL);
1729 if (!disksize)
1730 return -EINVAL;
1731
1732 down_write(&zram->init_lock);
1733 if (init_done(zram)) {
1734 pr_info("Cannot change disksize for initialized device\n");
1735 err = -EBUSY;
1736 goto out_unlock;
1737 }
1738
1739 disksize = PAGE_ALIGN(disksize);
1740 if (!zram_meta_alloc(zram, disksize)) {
1741 err = -ENOMEM;
1742 goto out_unlock;
1743 }
1744
1745 comp = zcomp_create(zram->compressor);
1746 if (IS_ERR(comp)) {
1747 pr_err("Cannot initialise %s compressing backend\n",
1748 zram->compressor);
1749 err = PTR_ERR(comp);
1750 goto out_free_meta;
1751 }
1752
1753 zram->comp = comp;
1754 zram->disksize = disksize;
1755 set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT);
1756 up_write(&zram->init_lock);
1757
1758 return len;
1759
1760 out_free_meta:
1761 zram_meta_free(zram, disksize);
1762 out_unlock:
1763 up_write(&zram->init_lock);
1764 return err;
1765 }
1766
1767 static ssize_t reset_store(struct device *dev,
1768 struct device_attribute *attr, const char *buf, size_t len)
1769 {
1770 int ret;
1771 unsigned short do_reset;
1772 struct zram *zram;
1773 struct block_device *bdev;
1774
1775 ret = kstrtou16(buf, 10, &do_reset);
1776 if (ret)
1777 return ret;
1778
1779 if (!do_reset)
1780 return -EINVAL;
1781
1782 zram = dev_to_zram(dev);
1783 bdev = zram->disk->part0;
1784
1785 mutex_lock(&bdev->bd_mutex);
1786 /* Do not reset an active device or claimed device */
1787 if (bdev->bd_openers || zram->claim) {
1788 mutex_unlock(&bdev->bd_mutex);
1789 return -EBUSY;
1790 }
1791
1792 /* From now on, anyone can't open /dev/zram[0-9] */
1793 zram->claim = true;
1794 mutex_unlock(&bdev->bd_mutex);
1795
1796 /* Make sure all the pending I/O are finished */
1797 fsync_bdev(bdev);
1798 zram_reset_device(zram);
1799
1800 mutex_lock(&bdev->bd_mutex);
1801 zram->claim = false;
1802 mutex_unlock(&bdev->bd_mutex);
1803
1804 return len;
1805 }
1806
1807 static int zram_open(struct block_device *bdev, fmode_t mode)
1808 {
1809 int ret = 0;
1810 struct zram *zram;
1811
1812 WARN_ON(!mutex_is_locked(&bdev->bd_mutex));
1813
1814 zram = bdev->bd_disk->private_data;
1815 /* zram was claimed to reset so open request fails */
1816 if (zram->claim)
1817 ret = -EBUSY;
1818
1819 return ret;
1820 }
1821
1822 static const struct block_device_operations zram_devops = {
1823 .open = zram_open,
1824 .submit_bio = zram_submit_bio,
1825 .swap_slot_free_notify = zram_slot_free_notify,
1826 .rw_page = zram_rw_page,
1827 .owner = THIS_MODULE
1828 };
1829
1830 static const struct block_device_operations zram_wb_devops = {
1831 .open = zram_open,
1832 .submit_bio = zram_submit_bio,
1833 .swap_slot_free_notify = zram_slot_free_notify,
1834 .owner = THIS_MODULE
1835 };
1836
1837 static DEVICE_ATTR_WO(compact);
1838 static DEVICE_ATTR_RW(disksize);
1839 static DEVICE_ATTR_RO(initstate);
1840 static DEVICE_ATTR_WO(reset);
1841 static DEVICE_ATTR_WO(mem_limit);
1842 static DEVICE_ATTR_WO(mem_used_max);
1843 static DEVICE_ATTR_WO(idle);
1844 static DEVICE_ATTR_RW(max_comp_streams);
1845 static DEVICE_ATTR_RW(comp_algorithm);
1846 #ifdef CONFIG_ZRAM_WRITEBACK
1847 static DEVICE_ATTR_RW(backing_dev);
1848 static DEVICE_ATTR_WO(writeback);
1849 static DEVICE_ATTR_RW(writeback_limit);
1850 static DEVICE_ATTR_RW(writeback_limit_enable);
1851 #endif
1852
1853 static struct attribute *zram_disk_attrs[] = {
1854 &dev_attr_disksize.attr,
1855 &dev_attr_initstate.attr,
1856 &dev_attr_reset.attr,
1857 &dev_attr_compact.attr,
1858 &dev_attr_mem_limit.attr,
1859 &dev_attr_mem_used_max.attr,
1860 &dev_attr_idle.attr,
1861 &dev_attr_max_comp_streams.attr,
1862 &dev_attr_comp_algorithm.attr,
1863 #ifdef CONFIG_ZRAM_WRITEBACK
1864 &dev_attr_backing_dev.attr,
1865 &dev_attr_writeback.attr,
1866 &dev_attr_writeback_limit.attr,
1867 &dev_attr_writeback_limit_enable.attr,
1868 #endif
1869 &dev_attr_io_stat.attr,
1870 &dev_attr_mm_stat.attr,
1871 #ifdef CONFIG_ZRAM_WRITEBACK
1872 &dev_attr_bd_stat.attr,
1873 #endif
1874 &dev_attr_debug_stat.attr,
1875 NULL,
1876 };
1877
1878 static const struct attribute_group zram_disk_attr_group = {
1879 .attrs = zram_disk_attrs,
1880 };
1881
1882 static const struct attribute_group *zram_disk_attr_groups[] = {
1883 &zram_disk_attr_group,
1884 NULL,
1885 };
1886
1887 /*
1888 * Allocate and initialize new zram device. the function returns
1889 * '>= 0' device_id upon success, and negative value otherwise.
1890 */
1891 static int zram_add(void)
1892 {
1893 struct zram *zram;
1894 struct request_queue *queue;
1895 int ret, device_id;
1896
1897 zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
1898 if (!zram)
1899 return -ENOMEM;
1900
1901 ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
1902 if (ret < 0)
1903 goto out_free_dev;
1904 device_id = ret;
1905
1906 init_rwsem(&zram->init_lock);
1907 #ifdef CONFIG_ZRAM_WRITEBACK
1908 spin_lock_init(&zram->wb_limit_lock);
1909 #endif
1910 queue = blk_alloc_queue(NUMA_NO_NODE);
1911 if (!queue) {
1912 pr_err("Error allocating disk queue for device %d\n",
1913 device_id);
1914 ret = -ENOMEM;
1915 goto out_free_idr;
1916 }
1917
1918 /* gendisk structure */
1919 zram->disk = alloc_disk(1);
1920 if (!zram->disk) {
1921 pr_err("Error allocating disk structure for device %d\n",
1922 device_id);
1923 ret = -ENOMEM;
1924 goto out_free_queue;
1925 }
1926
1927 zram->disk->major = zram_major;
1928 zram->disk->first_minor = device_id;
1929 zram->disk->fops = &zram_devops;
1930 zram->disk->queue = queue;
1931 zram->disk->private_data = zram;
1932 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
1933
1934 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
1935 set_capacity(zram->disk, 0);
1936 /* zram devices sort of resembles non-rotational disks */
1937 blk_queue_flag_set(QUEUE_FLAG_NONROT, zram->disk->queue);
1938 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
1939
1940 /*
1941 * To ensure that we always get PAGE_SIZE aligned
1942 * and n*PAGE_SIZED sized I/O requests.
1943 */
1944 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
1945 blk_queue_logical_block_size(zram->disk->queue,
1946 ZRAM_LOGICAL_BLOCK_SIZE);
1947 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1948 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
1949 zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1950 blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX);
1951 blk_queue_flag_set(QUEUE_FLAG_DISCARD, zram->disk->queue);
1952
1953 /*
1954 * zram_bio_discard() will clear all logical blocks if logical block
1955 * size is identical with physical block size(PAGE_SIZE). But if it is
1956 * different, we will skip discarding some parts of logical blocks in
1957 * the part of the request range which isn't aligned to physical block
1958 * size. So we can't ensure that all discarded logical blocks are
1959 * zeroed.
1960 */
1961 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1962 blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
1963
1964 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue);
1965 device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
1966
1967 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
1968
1969 zram_debugfs_register(zram);
1970 pr_info("Added device: %s\n", zram->disk->disk_name);
1971 return device_id;
1972
1973 out_free_queue:
1974 blk_cleanup_queue(queue);
1975 out_free_idr:
1976 idr_remove(&zram_index_idr, device_id);
1977 out_free_dev:
1978 kfree(zram);
1979 return ret;
1980 }
1981
1982 static int zram_remove(struct zram *zram)
1983 {
1984 struct block_device *bdev = zram->disk->part0;
1985
1986 mutex_lock(&bdev->bd_mutex);
1987 if (bdev->bd_openers || zram->claim) {
1988 mutex_unlock(&bdev->bd_mutex);
1989 return -EBUSY;
1990 }
1991
1992 zram->claim = true;
1993 mutex_unlock(&bdev->bd_mutex);
1994
1995 zram_debugfs_unregister(zram);
1996
1997 /* Make sure all the pending I/O are finished */
1998 fsync_bdev(bdev);
1999 zram_reset_device(zram);
2000
2001 pr_info("Removed device: %s\n", zram->disk->disk_name);
2002
2003 del_gendisk(zram->disk);
2004 blk_cleanup_queue(zram->disk->queue);
2005 put_disk(zram->disk);
2006 kfree(zram);
2007 return 0;
2008 }
2009
2010 /* zram-control sysfs attributes */
2011
2012 /*
2013 * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a
2014 * sense that reading from this file does alter the state of your system -- it
2015 * creates a new un-initialized zram device and returns back this device's
2016 * device_id (or an error code if it fails to create a new device).
2017 */
2018 static ssize_t hot_add_show(struct class *class,
2019 struct class_attribute *attr,
2020 char *buf)
2021 {
2022 int ret;
2023
2024 mutex_lock(&zram_index_mutex);
2025 ret = zram_add();
2026 mutex_unlock(&zram_index_mutex);
2027
2028 if (ret < 0)
2029 return ret;
2030 return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
2031 }
2032 static struct class_attribute class_attr_hot_add =
2033 __ATTR(hot_add, 0400, hot_add_show, NULL);
2034
2035 static ssize_t hot_remove_store(struct class *class,
2036 struct class_attribute *attr,
2037 const char *buf,
2038 size_t count)
2039 {
2040 struct zram *zram;
2041 int ret, dev_id;
2042
2043 /* dev_id is gendisk->first_minor, which is `int' */
2044 ret = kstrtoint(buf, 10, &dev_id);
2045 if (ret)
2046 return ret;
2047 if (dev_id < 0)
2048 return -EINVAL;
2049
2050 mutex_lock(&zram_index_mutex);
2051
2052 zram = idr_find(&zram_index_idr, dev_id);
2053 if (zram) {
2054 ret = zram_remove(zram);
2055 if (!ret)
2056 idr_remove(&zram_index_idr, dev_id);
2057 } else {
2058 ret = -ENODEV;
2059 }
2060
2061 mutex_unlock(&zram_index_mutex);
2062 return ret ? ret : count;
2063 }
2064 static CLASS_ATTR_WO(hot_remove);
2065
2066 static struct attribute *zram_control_class_attrs[] = {
2067 &class_attr_hot_add.attr,
2068 &class_attr_hot_remove.attr,
2069 NULL,
2070 };
2071 ATTRIBUTE_GROUPS(zram_control_class);
2072
2073 static struct class zram_control_class = {
2074 .name = "zram-control",
2075 .owner = THIS_MODULE,
2076 .class_groups = zram_control_class_groups,
2077 };
2078
2079 static int zram_remove_cb(int id, void *ptr, void *data)
2080 {
2081 zram_remove(ptr);
2082 return 0;
2083 }
2084
2085 static void destroy_devices(void)
2086 {
2087 class_unregister(&zram_control_class);
2088 idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
2089 zram_debugfs_destroy();
2090 idr_destroy(&zram_index_idr);
2091 unregister_blkdev(zram_major, "zram");
2092 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2093 }
2094
2095 static int __init zram_init(void)
2096 {
2097 int ret;
2098
2099 ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
2100 zcomp_cpu_up_prepare, zcomp_cpu_dead);
2101 if (ret < 0)
2102 return ret;
2103
2104 ret = class_register(&zram_control_class);
2105 if (ret) {
2106 pr_err("Unable to register zram-control class\n");
2107 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2108 return ret;
2109 }
2110
2111 zram_debugfs_create();
2112 zram_major = register_blkdev(0, "zram");
2113 if (zram_major <= 0) {
2114 pr_err("Unable to get major number\n");
2115 class_unregister(&zram_control_class);
2116 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2117 return -EBUSY;
2118 }
2119
2120 while (num_devices != 0) {
2121 mutex_lock(&zram_index_mutex);
2122 ret = zram_add();
2123 mutex_unlock(&zram_index_mutex);
2124 if (ret < 0)
2125 goto out_error;
2126 num_devices--;
2127 }
2128
2129 return 0;
2130
2131 out_error:
2132 destroy_devices();
2133 return ret;
2134 }
2135
2136 static void __exit zram_exit(void)
2137 {
2138 destroy_devices();
2139 }
2140
2141 module_init(zram_init);
2142 module_exit(zram_exit);
2143
2144 module_param(num_devices, uint, 0);
2145 MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
2146
2147 MODULE_LICENSE("Dual BSD/GPL");
2148 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
2149 MODULE_DESCRIPTION("Compressed RAM Block Device");