2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
5 * 2012, 2013 Minchan Kim
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the licence that better fits your requirements.
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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/highmem.h>
26 #include <linux/slab.h>
27 #include <linux/backing-dev.h>
28 #include <linux/string.h>
29 #include <linux/vmalloc.h>
30 #include <linux/err.h>
31 #include <linux/idr.h>
32 #include <linux/sysfs.h>
33 #include <linux/debugfs.h>
34 #include <linux/cpuhotplug.h>
35 #include <linux/part_stat.h>
39 static DEFINE_IDR(zram_index_idr);
40 /* idr index must be protected */
41 static DEFINE_MUTEX(zram_index_mutex);
43 static int zram_major;
44 static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
46 /* Module params (documentation at end) */
47 static unsigned int num_devices = 1;
49 * Pages that compress to sizes equals or greater than this are stored
50 * uncompressed in memory.
52 static size_t huge_class_size;
54 static const struct block_device_operations zram_devops;
56 static void zram_free_page(struct zram *zram, size_t index);
57 static int zram_read_page(struct zram *zram, struct page *page, u32 index,
60 static int zram_slot_trylock(struct zram *zram, u32 index)
62 return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags);
65 static void zram_slot_lock(struct zram *zram, u32 index)
67 bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags);
70 static void zram_slot_unlock(struct zram *zram, u32 index)
72 bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags);
75 static inline bool init_done(struct zram *zram)
77 return zram->disksize;
80 static inline struct zram *dev_to_zram(struct device *dev)
82 return (struct zram *)dev_to_disk(dev)->private_data;
85 static unsigned long zram_get_handle(struct zram *zram, u32 index)
87 return zram->table[index].handle;
90 static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
92 zram->table[index].handle = handle;
95 /* flag operations require table entry bit_spin_lock() being held */
96 static bool zram_test_flag(struct zram *zram, u32 index,
97 enum zram_pageflags flag)
99 return zram->table[index].flags & BIT(flag);
102 static void zram_set_flag(struct zram *zram, u32 index,
103 enum zram_pageflags flag)
105 zram->table[index].flags |= BIT(flag);
108 static void zram_clear_flag(struct zram *zram, u32 index,
109 enum zram_pageflags flag)
111 zram->table[index].flags &= ~BIT(flag);
114 static inline void zram_set_element(struct zram *zram, u32 index,
115 unsigned long element)
117 zram->table[index].element = element;
120 static unsigned long zram_get_element(struct zram *zram, u32 index)
122 return zram->table[index].element;
125 static size_t zram_get_obj_size(struct zram *zram, u32 index)
127 return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
130 static void zram_set_obj_size(struct zram *zram,
131 u32 index, size_t size)
133 unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
135 zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
138 static inline bool zram_allocated(struct zram *zram, u32 index)
140 return zram_get_obj_size(zram, index) ||
141 zram_test_flag(zram, index, ZRAM_SAME) ||
142 zram_test_flag(zram, index, ZRAM_WB);
145 #if PAGE_SIZE != 4096
146 static inline bool is_partial_io(struct bio_vec *bvec)
148 return bvec->bv_len != PAGE_SIZE;
150 #define ZRAM_PARTIAL_IO 1
152 static inline bool is_partial_io(struct bio_vec *bvec)
158 static inline void zram_set_priority(struct zram *zram, u32 index, u32 prio)
160 prio &= ZRAM_COMP_PRIORITY_MASK;
162 * Clear previous priority value first, in case if we recompress
163 * further an already recompressed page
165 zram->table[index].flags &= ~(ZRAM_COMP_PRIORITY_MASK <<
166 ZRAM_COMP_PRIORITY_BIT1);
167 zram->table[index].flags |= (prio << ZRAM_COMP_PRIORITY_BIT1);
170 static inline u32 zram_get_priority(struct zram *zram, u32 index)
172 u32 prio = zram->table[index].flags >> ZRAM_COMP_PRIORITY_BIT1;
174 return prio & ZRAM_COMP_PRIORITY_MASK;
177 static void zram_accessed(struct zram *zram, u32 index)
179 zram_clear_flag(zram, index, ZRAM_IDLE);
180 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
181 zram->table[index].ac_time = ktime_get_boottime();
185 static inline void update_used_max(struct zram *zram,
186 const unsigned long pages)
188 unsigned long cur_max = atomic_long_read(&zram->stats.max_used_pages);
191 if (cur_max >= pages)
193 } while (!atomic_long_try_cmpxchg(&zram->stats.max_used_pages,
197 static inline void zram_fill_page(void *ptr, unsigned long len,
200 WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
201 memset_l(ptr, value, len / sizeof(unsigned long));
204 static bool page_same_filled(void *ptr, unsigned long *element)
208 unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
210 page = (unsigned long *)ptr;
213 if (val != page[last_pos])
216 for (pos = 1; pos < last_pos; pos++) {
217 if (val != page[pos])
226 static ssize_t initstate_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
230 struct zram *zram = dev_to_zram(dev);
232 down_read(&zram->init_lock);
233 val = init_done(zram);
234 up_read(&zram->init_lock);
236 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
239 static ssize_t disksize_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct zram *zram = dev_to_zram(dev);
244 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
247 static ssize_t mem_limit_store(struct device *dev,
248 struct device_attribute *attr, const char *buf, size_t len)
252 struct zram *zram = dev_to_zram(dev);
254 limit = memparse(buf, &tmp);
255 if (buf == tmp) /* no chars parsed, invalid input */
258 down_write(&zram->init_lock);
259 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
260 up_write(&zram->init_lock);
265 static ssize_t mem_used_max_store(struct device *dev,
266 struct device_attribute *attr, const char *buf, size_t len)
270 struct zram *zram = dev_to_zram(dev);
272 err = kstrtoul(buf, 10, &val);
276 down_read(&zram->init_lock);
277 if (init_done(zram)) {
278 atomic_long_set(&zram->stats.max_used_pages,
279 zs_get_total_pages(zram->mem_pool));
281 up_read(&zram->init_lock);
287 * Mark all pages which are older than or equal to cutoff as IDLE.
288 * Callers should hold the zram init lock in read mode
290 static void mark_idle(struct zram *zram, ktime_t cutoff)
293 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
296 for (index = 0; index < nr_pages; index++) {
298 * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
299 * See the comment in writeback_store.
301 zram_slot_lock(zram, index);
302 if (zram_allocated(zram, index) &&
303 !zram_test_flag(zram, index, ZRAM_UNDER_WB)) {
304 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
305 is_idle = !cutoff || ktime_after(cutoff,
306 zram->table[index].ac_time);
309 zram_set_flag(zram, index, ZRAM_IDLE);
311 zram_slot_unlock(zram, index);
315 static ssize_t idle_store(struct device *dev,
316 struct device_attribute *attr, const char *buf, size_t len)
318 struct zram *zram = dev_to_zram(dev);
319 ktime_t cutoff_time = 0;
320 ssize_t rv = -EINVAL;
322 if (!sysfs_streq(buf, "all")) {
324 * If it did not parse as 'all' try to treat it as an integer
325 * when we have memory tracking enabled.
329 if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && !kstrtoull(buf, 0, &age_sec))
330 cutoff_time = ktime_sub(ktime_get_boottime(),
331 ns_to_ktime(age_sec * NSEC_PER_SEC));
336 down_read(&zram->init_lock);
337 if (!init_done(zram))
341 * A cutoff_time of 0 marks everything as idle, this is the
344 mark_idle(zram, cutoff_time);
348 up_read(&zram->init_lock);
353 #ifdef CONFIG_ZRAM_WRITEBACK
354 static ssize_t writeback_limit_enable_store(struct device *dev,
355 struct device_attribute *attr, const char *buf, size_t len)
357 struct zram *zram = dev_to_zram(dev);
359 ssize_t ret = -EINVAL;
361 if (kstrtoull(buf, 10, &val))
364 down_read(&zram->init_lock);
365 spin_lock(&zram->wb_limit_lock);
366 zram->wb_limit_enable = val;
367 spin_unlock(&zram->wb_limit_lock);
368 up_read(&zram->init_lock);
374 static ssize_t writeback_limit_enable_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
378 struct zram *zram = dev_to_zram(dev);
380 down_read(&zram->init_lock);
381 spin_lock(&zram->wb_limit_lock);
382 val = zram->wb_limit_enable;
383 spin_unlock(&zram->wb_limit_lock);
384 up_read(&zram->init_lock);
386 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
389 static ssize_t writeback_limit_store(struct device *dev,
390 struct device_attribute *attr, const char *buf, size_t len)
392 struct zram *zram = dev_to_zram(dev);
394 ssize_t ret = -EINVAL;
396 if (kstrtoull(buf, 10, &val))
399 down_read(&zram->init_lock);
400 spin_lock(&zram->wb_limit_lock);
401 zram->bd_wb_limit = val;
402 spin_unlock(&zram->wb_limit_lock);
403 up_read(&zram->init_lock);
409 static ssize_t writeback_limit_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
413 struct zram *zram = dev_to_zram(dev);
415 down_read(&zram->init_lock);
416 spin_lock(&zram->wb_limit_lock);
417 val = zram->bd_wb_limit;
418 spin_unlock(&zram->wb_limit_lock);
419 up_read(&zram->init_lock);
421 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
424 static void reset_bdev(struct zram *zram)
426 if (!zram->backing_dev)
429 fput(zram->bdev_file);
430 /* hope filp_close flush all of IO */
431 filp_close(zram->backing_dev, NULL);
432 zram->backing_dev = NULL;
433 zram->bdev_file = NULL;
434 zram->disk->fops = &zram_devops;
435 kvfree(zram->bitmap);
439 static ssize_t backing_dev_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
443 struct zram *zram = dev_to_zram(dev);
447 down_read(&zram->init_lock);
448 file = zram->backing_dev;
450 memcpy(buf, "none\n", 5);
451 up_read(&zram->init_lock);
455 p = file_path(file, buf, PAGE_SIZE - 1);
462 memmove(buf, p, ret);
465 up_read(&zram->init_lock);
469 static ssize_t backing_dev_store(struct device *dev,
470 struct device_attribute *attr, const char *buf, size_t len)
474 struct file *backing_dev = NULL;
476 struct address_space *mapping;
477 unsigned int bitmap_sz;
478 unsigned long nr_pages, *bitmap = NULL;
479 struct file *bdev_file = NULL;
481 struct zram *zram = dev_to_zram(dev);
483 file_name = kmalloc(PATH_MAX, GFP_KERNEL);
487 down_write(&zram->init_lock);
488 if (init_done(zram)) {
489 pr_info("Can't setup backing device for initialized device\n");
494 strscpy(file_name, buf, PATH_MAX);
495 /* ignore trailing newline */
496 sz = strlen(file_name);
497 if (sz > 0 && file_name[sz - 1] == '\n')
498 file_name[sz - 1] = 0x00;
500 backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
501 if (IS_ERR(backing_dev)) {
502 err = PTR_ERR(backing_dev);
507 mapping = backing_dev->f_mapping;
508 inode = mapping->host;
510 /* Support only block device in this moment */
511 if (!S_ISBLK(inode->i_mode)) {
516 bdev_file = bdev_file_open_by_dev(inode->i_rdev,
517 BLK_OPEN_READ | BLK_OPEN_WRITE, zram, NULL);
518 if (IS_ERR(bdev_file)) {
519 err = PTR_ERR(bdev_file);
524 nr_pages = i_size_read(inode) >> PAGE_SHIFT;
525 bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
526 bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
534 zram->bdev_file = bdev_file;
535 zram->backing_dev = backing_dev;
536 zram->bitmap = bitmap;
537 zram->nr_pages = nr_pages;
538 up_write(&zram->init_lock);
540 pr_info("setup backing device %s\n", file_name);
551 filp_close(backing_dev, NULL);
553 up_write(&zram->init_lock);
560 static unsigned long alloc_block_bdev(struct zram *zram)
562 unsigned long blk_idx = 1;
564 /* skip 0 bit to confuse zram.handle = 0 */
565 blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
566 if (blk_idx == zram->nr_pages)
569 if (test_and_set_bit(blk_idx, zram->bitmap))
572 atomic64_inc(&zram->stats.bd_count);
576 static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
580 was_set = test_and_clear_bit(blk_idx, zram->bitmap);
581 WARN_ON_ONCE(!was_set);
582 atomic64_dec(&zram->stats.bd_count);
585 static void read_from_bdev_async(struct zram *zram, struct page *page,
586 unsigned long entry, struct bio *parent)
590 bio = bio_alloc(file_bdev(zram->bdev_file), 1, parent->bi_opf, GFP_NOIO);
591 bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
592 __bio_add_page(bio, page, PAGE_SIZE, 0);
593 bio_chain(bio, parent);
597 #define PAGE_WB_SIG "page_index="
599 #define PAGE_WRITEBACK 0
600 #define HUGE_WRITEBACK (1<<0)
601 #define IDLE_WRITEBACK (1<<1)
602 #define INCOMPRESSIBLE_WRITEBACK (1<<2)
604 static ssize_t writeback_store(struct device *dev,
605 struct device_attribute *attr, const char *buf, size_t len)
607 struct zram *zram = dev_to_zram(dev);
608 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
609 unsigned long index = 0;
611 struct bio_vec bio_vec;
615 unsigned long blk_idx = 0;
617 if (sysfs_streq(buf, "idle"))
618 mode = IDLE_WRITEBACK;
619 else if (sysfs_streq(buf, "huge"))
620 mode = HUGE_WRITEBACK;
621 else if (sysfs_streq(buf, "huge_idle"))
622 mode = IDLE_WRITEBACK | HUGE_WRITEBACK;
623 else if (sysfs_streq(buf, "incompressible"))
624 mode = INCOMPRESSIBLE_WRITEBACK;
626 if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1))
629 if (kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index) ||
634 mode = PAGE_WRITEBACK;
637 down_read(&zram->init_lock);
638 if (!init_done(zram)) {
640 goto release_init_lock;
643 if (!zram->backing_dev) {
645 goto release_init_lock;
648 page = alloc_page(GFP_KERNEL);
651 goto release_init_lock;
654 for (; nr_pages != 0; index++, nr_pages--) {
655 spin_lock(&zram->wb_limit_lock);
656 if (zram->wb_limit_enable && !zram->bd_wb_limit) {
657 spin_unlock(&zram->wb_limit_lock);
661 spin_unlock(&zram->wb_limit_lock);
664 blk_idx = alloc_block_bdev(zram);
671 zram_slot_lock(zram, index);
672 if (!zram_allocated(zram, index))
675 if (zram_test_flag(zram, index, ZRAM_WB) ||
676 zram_test_flag(zram, index, ZRAM_SAME) ||
677 zram_test_flag(zram, index, ZRAM_UNDER_WB))
680 if (mode & IDLE_WRITEBACK &&
681 !zram_test_flag(zram, index, ZRAM_IDLE))
683 if (mode & HUGE_WRITEBACK &&
684 !zram_test_flag(zram, index, ZRAM_HUGE))
686 if (mode & INCOMPRESSIBLE_WRITEBACK &&
687 !zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
691 * Clearing ZRAM_UNDER_WB is duty of caller.
692 * IOW, zram_free_page never clear it.
694 zram_set_flag(zram, index, ZRAM_UNDER_WB);
695 /* Need for hugepage writeback racing */
696 zram_set_flag(zram, index, ZRAM_IDLE);
697 zram_slot_unlock(zram, index);
698 if (zram_read_page(zram, page, index, NULL)) {
699 zram_slot_lock(zram, index);
700 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
701 zram_clear_flag(zram, index, ZRAM_IDLE);
702 zram_slot_unlock(zram, index);
706 bio_init(&bio, file_bdev(zram->bdev_file), &bio_vec, 1,
707 REQ_OP_WRITE | REQ_SYNC);
708 bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
709 __bio_add_page(&bio, page, PAGE_SIZE, 0);
712 * XXX: A single page IO would be inefficient for write
713 * but it would be not bad as starter.
715 err = submit_bio_wait(&bio);
717 zram_slot_lock(zram, index);
718 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
719 zram_clear_flag(zram, index, ZRAM_IDLE);
720 zram_slot_unlock(zram, index);
722 * BIO errors are not fatal, we continue and simply
723 * attempt to writeback the remaining objects (pages).
724 * At the same time we need to signal user-space that
725 * some writes (at least one, but also could be all of
726 * them) were not successful and we do so by returning
727 * the most recent BIO error.
733 atomic64_inc(&zram->stats.bd_writes);
735 * We released zram_slot_lock so need to check if the slot was
736 * changed. If there is freeing for the slot, we can catch it
737 * easily by zram_allocated.
738 * A subtle case is the slot is freed/reallocated/marked as
739 * ZRAM_IDLE again. To close the race, idle_store doesn't
740 * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
741 * Thus, we could close the race by checking ZRAM_IDLE bit.
743 zram_slot_lock(zram, index);
744 if (!zram_allocated(zram, index) ||
745 !zram_test_flag(zram, index, ZRAM_IDLE)) {
746 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
747 zram_clear_flag(zram, index, ZRAM_IDLE);
751 zram_free_page(zram, index);
752 zram_clear_flag(zram, index, ZRAM_UNDER_WB);
753 zram_set_flag(zram, index, ZRAM_WB);
754 zram_set_element(zram, index, blk_idx);
756 atomic64_inc(&zram->stats.pages_stored);
757 spin_lock(&zram->wb_limit_lock);
758 if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
759 zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
760 spin_unlock(&zram->wb_limit_lock);
762 zram_slot_unlock(zram, index);
766 free_block_bdev(zram, blk_idx);
769 up_read(&zram->init_lock);
775 struct work_struct work;
782 static void zram_sync_read(struct work_struct *work)
784 struct zram_work *zw = container_of(work, struct zram_work, work);
788 bio_init(&bio, file_bdev(zw->zram->bdev_file), &bv, 1, REQ_OP_READ);
789 bio.bi_iter.bi_sector = zw->entry * (PAGE_SIZE >> 9);
790 __bio_add_page(&bio, zw->page, PAGE_SIZE, 0);
791 zw->error = submit_bio_wait(&bio);
795 * Block layer want one ->submit_bio to be active at a time, so if we use
796 * chained IO with parent IO in same context, it's a deadlock. To avoid that,
797 * use a worker thread context.
799 static int read_from_bdev_sync(struct zram *zram, struct page *page,
802 struct zram_work work;
808 INIT_WORK_ONSTACK(&work.work, zram_sync_read);
809 queue_work(system_unbound_wq, &work.work);
810 flush_work(&work.work);
811 destroy_work_on_stack(&work.work);
816 static int read_from_bdev(struct zram *zram, struct page *page,
817 unsigned long entry, struct bio *parent)
819 atomic64_inc(&zram->stats.bd_reads);
821 if (WARN_ON_ONCE(!IS_ENABLED(ZRAM_PARTIAL_IO)))
823 return read_from_bdev_sync(zram, page, entry);
825 read_from_bdev_async(zram, page, entry, parent);
829 static inline void reset_bdev(struct zram *zram) {};
830 static int read_from_bdev(struct zram *zram, struct page *page,
831 unsigned long entry, struct bio *parent)
836 static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
839 #ifdef CONFIG_ZRAM_MEMORY_TRACKING
841 static struct dentry *zram_debugfs_root;
843 static void zram_debugfs_create(void)
845 zram_debugfs_root = debugfs_create_dir("zram", NULL);
848 static void zram_debugfs_destroy(void)
850 debugfs_remove_recursive(zram_debugfs_root);
853 static ssize_t read_block_state(struct file *file, char __user *buf,
854 size_t count, loff_t *ppos)
857 ssize_t index, written = 0;
858 struct zram *zram = file->private_data;
859 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
860 struct timespec64 ts;
862 kbuf = kvmalloc(count, GFP_KERNEL);
866 down_read(&zram->init_lock);
867 if (!init_done(zram)) {
868 up_read(&zram->init_lock);
873 for (index = *ppos; index < nr_pages; index++) {
876 zram_slot_lock(zram, index);
877 if (!zram_allocated(zram, index))
880 ts = ktime_to_timespec64(zram->table[index].ac_time);
881 copied = snprintf(kbuf + written, count,
882 "%12zd %12lld.%06lu %c%c%c%c%c%c\n",
883 index, (s64)ts.tv_sec,
884 ts.tv_nsec / NSEC_PER_USEC,
885 zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
886 zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
887 zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
888 zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.',
889 zram_get_priority(zram, index) ? 'r' : '.',
890 zram_test_flag(zram, index,
891 ZRAM_INCOMPRESSIBLE) ? 'n' : '.');
893 if (count <= copied) {
894 zram_slot_unlock(zram, index);
900 zram_slot_unlock(zram, index);
904 up_read(&zram->init_lock);
905 if (copy_to_user(buf, kbuf, written))
912 static const struct file_operations proc_zram_block_state_op = {
914 .read = read_block_state,
915 .llseek = default_llseek,
918 static void zram_debugfs_register(struct zram *zram)
920 if (!zram_debugfs_root)
923 zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
925 debugfs_create_file("block_state", 0400, zram->debugfs_dir,
926 zram, &proc_zram_block_state_op);
929 static void zram_debugfs_unregister(struct zram *zram)
931 debugfs_remove_recursive(zram->debugfs_dir);
934 static void zram_debugfs_create(void) {};
935 static void zram_debugfs_destroy(void) {};
936 static void zram_debugfs_register(struct zram *zram) {};
937 static void zram_debugfs_unregister(struct zram *zram) {};
941 * We switched to per-cpu streams and this attr is not needed anymore.
942 * However, we will keep it around for some time, because:
943 * a) we may revert per-cpu streams in the future
944 * b) it's visible to user space and we need to follow our 2 years
945 * retirement rule; but we already have a number of 'soon to be
946 * altered' attrs, so max_comp_streams need to wait for the next
949 static ssize_t max_comp_streams_show(struct device *dev,
950 struct device_attribute *attr, char *buf)
952 return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
955 static ssize_t max_comp_streams_store(struct device *dev,
956 struct device_attribute *attr, const char *buf, size_t len)
961 static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
963 /* Do not free statically defined compression algorithms */
964 if (zram->comp_algs[prio] != default_compressor)
965 kfree(zram->comp_algs[prio]);
967 zram->comp_algs[prio] = alg;
970 static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, char *buf)
974 down_read(&zram->init_lock);
975 sz = zcomp_available_show(zram->comp_algs[prio], buf);
976 up_read(&zram->init_lock);
981 static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
987 if (sz >= CRYPTO_MAX_ALG_NAME)
990 compressor = kstrdup(buf, GFP_KERNEL);
994 /* ignore trailing newline */
995 if (sz > 0 && compressor[sz - 1] == '\n')
996 compressor[sz - 1] = 0x00;
998 if (!zcomp_available_algorithm(compressor)) {
1003 down_write(&zram->init_lock);
1004 if (init_done(zram)) {
1005 up_write(&zram->init_lock);
1007 pr_info("Can't change algorithm for initialized device\n");
1011 comp_algorithm_set(zram, prio, compressor);
1012 up_write(&zram->init_lock);
1016 static ssize_t comp_algorithm_show(struct device *dev,
1017 struct device_attribute *attr,
1020 struct zram *zram = dev_to_zram(dev);
1022 return __comp_algorithm_show(zram, ZRAM_PRIMARY_COMP, buf);
1025 static ssize_t comp_algorithm_store(struct device *dev,
1026 struct device_attribute *attr,
1030 struct zram *zram = dev_to_zram(dev);
1033 ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, buf);
1034 return ret ? ret : len;
1037 #ifdef CONFIG_ZRAM_MULTI_COMP
1038 static ssize_t recomp_algorithm_show(struct device *dev,
1039 struct device_attribute *attr,
1042 struct zram *zram = dev_to_zram(dev);
1046 for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
1047 if (!zram->comp_algs[prio])
1050 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, "#%d: ", prio);
1051 sz += __comp_algorithm_show(zram, prio, buf + sz);
1057 static ssize_t recomp_algorithm_store(struct device *dev,
1058 struct device_attribute *attr,
1062 struct zram *zram = dev_to_zram(dev);
1063 int prio = ZRAM_SECONDARY_COMP;
1064 char *args, *param, *val;
1068 args = skip_spaces(buf);
1070 args = next_arg(args, ¶m, &val);
1075 if (!strcmp(param, "algo")) {
1080 if (!strcmp(param, "priority")) {
1081 ret = kstrtoint(val, 10, &prio);
1091 if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS)
1094 ret = __comp_algorithm_store(zram, prio, alg);
1095 return ret ? ret : len;
1099 static ssize_t compact_store(struct device *dev,
1100 struct device_attribute *attr, const char *buf, size_t len)
1102 struct zram *zram = dev_to_zram(dev);
1104 down_read(&zram->init_lock);
1105 if (!init_done(zram)) {
1106 up_read(&zram->init_lock);
1110 zs_compact(zram->mem_pool);
1111 up_read(&zram->init_lock);
1116 static ssize_t io_stat_show(struct device *dev,
1117 struct device_attribute *attr, char *buf)
1119 struct zram *zram = dev_to_zram(dev);
1122 down_read(&zram->init_lock);
1123 ret = scnprintf(buf, PAGE_SIZE,
1124 "%8llu %8llu 0 %8llu\n",
1125 (u64)atomic64_read(&zram->stats.failed_reads),
1126 (u64)atomic64_read(&zram->stats.failed_writes),
1127 (u64)atomic64_read(&zram->stats.notify_free));
1128 up_read(&zram->init_lock);
1133 static ssize_t mm_stat_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
1136 struct zram *zram = dev_to_zram(dev);
1137 struct zs_pool_stats pool_stats;
1138 u64 orig_size, mem_used = 0;
1142 memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
1144 down_read(&zram->init_lock);
1145 if (init_done(zram)) {
1146 mem_used = zs_get_total_pages(zram->mem_pool);
1147 zs_pool_stats(zram->mem_pool, &pool_stats);
1150 orig_size = atomic64_read(&zram->stats.pages_stored);
1151 max_used = atomic_long_read(&zram->stats.max_used_pages);
1153 ret = scnprintf(buf, PAGE_SIZE,
1154 "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
1155 orig_size << PAGE_SHIFT,
1156 (u64)atomic64_read(&zram->stats.compr_data_size),
1157 mem_used << PAGE_SHIFT,
1158 zram->limit_pages << PAGE_SHIFT,
1159 max_used << PAGE_SHIFT,
1160 (u64)atomic64_read(&zram->stats.same_pages),
1161 atomic_long_read(&pool_stats.pages_compacted),
1162 (u64)atomic64_read(&zram->stats.huge_pages),
1163 (u64)atomic64_read(&zram->stats.huge_pages_since));
1164 up_read(&zram->init_lock);
1169 #ifdef CONFIG_ZRAM_WRITEBACK
1170 #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
1171 static ssize_t bd_stat_show(struct device *dev,
1172 struct device_attribute *attr, char *buf)
1174 struct zram *zram = dev_to_zram(dev);
1177 down_read(&zram->init_lock);
1178 ret = scnprintf(buf, PAGE_SIZE,
1179 "%8llu %8llu %8llu\n",
1180 FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
1181 FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
1182 FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
1183 up_read(&zram->init_lock);
1189 static ssize_t debug_stat_show(struct device *dev,
1190 struct device_attribute *attr, char *buf)
1193 struct zram *zram = dev_to_zram(dev);
1196 down_read(&zram->init_lock);
1197 ret = scnprintf(buf, PAGE_SIZE,
1198 "version: %d\n%8llu %8llu\n",
1200 (u64)atomic64_read(&zram->stats.writestall),
1201 (u64)atomic64_read(&zram->stats.miss_free));
1202 up_read(&zram->init_lock);
1207 static DEVICE_ATTR_RO(io_stat);
1208 static DEVICE_ATTR_RO(mm_stat);
1209 #ifdef CONFIG_ZRAM_WRITEBACK
1210 static DEVICE_ATTR_RO(bd_stat);
1212 static DEVICE_ATTR_RO(debug_stat);
1214 static void zram_meta_free(struct zram *zram, u64 disksize)
1216 size_t num_pages = disksize >> PAGE_SHIFT;
1219 /* Free all pages that are still in this zram device */
1220 for (index = 0; index < num_pages; index++)
1221 zram_free_page(zram, index);
1223 zs_destroy_pool(zram->mem_pool);
1227 static bool zram_meta_alloc(struct zram *zram, u64 disksize)
1231 num_pages = disksize >> PAGE_SHIFT;
1232 zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
1236 zram->mem_pool = zs_create_pool(zram->disk->disk_name);
1237 if (!zram->mem_pool) {
1242 if (!huge_class_size)
1243 huge_class_size = zs_huge_class_size(zram->mem_pool);
1248 * To protect concurrent access to the same index entry,
1249 * caller should hold this table index entry's bit_spinlock to
1250 * indicate this index entry is accessing.
1252 static void zram_free_page(struct zram *zram, size_t index)
1254 unsigned long handle;
1256 #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
1257 zram->table[index].ac_time = 0;
1259 if (zram_test_flag(zram, index, ZRAM_IDLE))
1260 zram_clear_flag(zram, index, ZRAM_IDLE);
1262 if (zram_test_flag(zram, index, ZRAM_HUGE)) {
1263 zram_clear_flag(zram, index, ZRAM_HUGE);
1264 atomic64_dec(&zram->stats.huge_pages);
1267 if (zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
1268 zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE);
1270 zram_set_priority(zram, index, 0);
1272 if (zram_test_flag(zram, index, ZRAM_WB)) {
1273 zram_clear_flag(zram, index, ZRAM_WB);
1274 free_block_bdev(zram, zram_get_element(zram, index));
1279 * No memory is allocated for same element filled pages.
1280 * Simply clear same page flag.
1282 if (zram_test_flag(zram, index, ZRAM_SAME)) {
1283 zram_clear_flag(zram, index, ZRAM_SAME);
1284 atomic64_dec(&zram->stats.same_pages);
1288 handle = zram_get_handle(zram, index);
1292 zs_free(zram->mem_pool, handle);
1294 atomic64_sub(zram_get_obj_size(zram, index),
1295 &zram->stats.compr_data_size);
1297 atomic64_dec(&zram->stats.pages_stored);
1298 zram_set_handle(zram, index, 0);
1299 zram_set_obj_size(zram, index, 0);
1300 WARN_ON_ONCE(zram->table[index].flags &
1301 ~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB));
1305 * Reads (decompresses if needed) a page from zspool (zsmalloc).
1306 * Corresponding ZRAM slot should be locked.
1308 static int zram_read_from_zspool(struct zram *zram, struct page *page,
1311 struct zcomp_strm *zstrm;
1312 unsigned long handle;
1318 handle = zram_get_handle(zram, index);
1319 if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
1320 unsigned long value;
1323 value = handle ? zram_get_element(zram, index) : 0;
1324 mem = kmap_local_page(page);
1325 zram_fill_page(mem, PAGE_SIZE, value);
1330 size = zram_get_obj_size(zram, index);
1332 if (size != PAGE_SIZE) {
1333 prio = zram_get_priority(zram, index);
1334 zstrm = zcomp_stream_get(zram->comps[prio]);
1337 src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
1338 if (size == PAGE_SIZE) {
1339 dst = kmap_local_page(page);
1340 memcpy(dst, src, PAGE_SIZE);
1344 dst = kmap_local_page(page);
1345 ret = zcomp_decompress(zstrm, src, size, dst);
1347 zcomp_stream_put(zram->comps[prio]);
1349 zs_unmap_object(zram->mem_pool, handle);
1353 static int zram_read_page(struct zram *zram, struct page *page, u32 index,
1358 zram_slot_lock(zram, index);
1359 if (!zram_test_flag(zram, index, ZRAM_WB)) {
1360 /* Slot should be locked through out the function call */
1361 ret = zram_read_from_zspool(zram, page, index);
1362 zram_slot_unlock(zram, index);
1365 * The slot should be unlocked before reading from the backing
1368 zram_slot_unlock(zram, index);
1370 ret = read_from_bdev(zram, page, zram_get_element(zram, index),
1374 /* Should NEVER happen. Return bio error if it does. */
1375 if (WARN_ON(ret < 0))
1376 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
1382 * Use a temporary buffer to decompress the page, as the decompressor
1383 * always expects a full page for the output.
1385 static int zram_bvec_read_partial(struct zram *zram, struct bio_vec *bvec,
1386 u32 index, int offset)
1388 struct page *page = alloc_page(GFP_NOIO);
1393 ret = zram_read_page(zram, page, index, NULL);
1395 memcpy_to_bvec(bvec, page_address(page) + offset);
1400 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
1401 u32 index, int offset, struct bio *bio)
1403 if (is_partial_io(bvec))
1404 return zram_bvec_read_partial(zram, bvec, index, offset);
1405 return zram_read_page(zram, bvec->bv_page, index, bio);
1408 static int zram_write_page(struct zram *zram, struct page *page, u32 index)
1411 unsigned long alloced_pages;
1412 unsigned long handle = -ENOMEM;
1413 unsigned int comp_len = 0;
1414 void *src, *dst, *mem;
1415 struct zcomp_strm *zstrm;
1416 unsigned long element = 0;
1417 enum zram_pageflags flags = 0;
1419 mem = kmap_local_page(page);
1420 if (page_same_filled(mem, &element)) {
1422 /* Free memory associated with this sector now. */
1424 atomic64_inc(&zram->stats.same_pages);
1430 zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
1431 src = kmap_local_page(page);
1432 ret = zcomp_compress(zstrm, src, &comp_len);
1435 if (unlikely(ret)) {
1436 zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1437 pr_err("Compression failed! err=%d\n", ret);
1438 zs_free(zram->mem_pool, handle);
1442 if (comp_len >= huge_class_size)
1443 comp_len = PAGE_SIZE;
1445 * handle allocation has 2 paths:
1446 * a) fast path is executed with preemption disabled (for
1447 * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear,
1448 * since we can't sleep;
1449 * b) slow path enables preemption and attempts to allocate
1450 * the page with __GFP_DIRECT_RECLAIM bit set. we have to
1451 * put per-cpu compression stream and, thus, to re-do
1452 * the compression once handle is allocated.
1454 * if we have a 'non-null' handle here then we are coming
1455 * from the slow path and handle has already been allocated.
1457 if (IS_ERR_VALUE(handle))
1458 handle = zs_malloc(zram->mem_pool, comp_len,
1459 __GFP_KSWAPD_RECLAIM |
1463 if (IS_ERR_VALUE(handle)) {
1464 zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1465 atomic64_inc(&zram->stats.writestall);
1466 handle = zs_malloc(zram->mem_pool, comp_len,
1467 GFP_NOIO | __GFP_HIGHMEM |
1469 if (IS_ERR_VALUE(handle))
1470 return PTR_ERR((void *)handle);
1472 if (comp_len != PAGE_SIZE)
1473 goto compress_again;
1475 * If the page is not compressible, you need to acquire the
1476 * lock and execute the code below. The zcomp_stream_get()
1477 * call is needed to disable the cpu hotplug and grab the
1478 * zstrm buffer back. It is necessary that the dereferencing
1479 * of the zstrm variable below occurs correctly.
1481 zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
1484 alloced_pages = zs_get_total_pages(zram->mem_pool);
1485 update_used_max(zram, alloced_pages);
1487 if (zram->limit_pages && alloced_pages > zram->limit_pages) {
1488 zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1489 zs_free(zram->mem_pool, handle);
1493 dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
1495 src = zstrm->buffer;
1496 if (comp_len == PAGE_SIZE)
1497 src = kmap_local_page(page);
1498 memcpy(dst, src, comp_len);
1499 if (comp_len == PAGE_SIZE)
1502 zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1503 zs_unmap_object(zram->mem_pool, handle);
1504 atomic64_add(comp_len, &zram->stats.compr_data_size);
1507 * Free memory associated with this sector
1508 * before overwriting unused sectors.
1510 zram_slot_lock(zram, index);
1511 zram_free_page(zram, index);
1513 if (comp_len == PAGE_SIZE) {
1514 zram_set_flag(zram, index, ZRAM_HUGE);
1515 atomic64_inc(&zram->stats.huge_pages);
1516 atomic64_inc(&zram->stats.huge_pages_since);
1520 zram_set_flag(zram, index, flags);
1521 zram_set_element(zram, index, element);
1523 zram_set_handle(zram, index, handle);
1524 zram_set_obj_size(zram, index, comp_len);
1526 zram_slot_unlock(zram, index);
1529 atomic64_inc(&zram->stats.pages_stored);
1534 * This is a partial IO. Read the full page before writing the changes.
1536 static int zram_bvec_write_partial(struct zram *zram, struct bio_vec *bvec,
1537 u32 index, int offset, struct bio *bio)
1539 struct page *page = alloc_page(GFP_NOIO);
1545 ret = zram_read_page(zram, page, index, bio);
1547 memcpy_from_bvec(page_address(page) + offset, bvec);
1548 ret = zram_write_page(zram, page, index);
1554 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1555 u32 index, int offset, struct bio *bio)
1557 if (is_partial_io(bvec))
1558 return zram_bvec_write_partial(zram, bvec, index, offset, bio);
1559 return zram_write_page(zram, bvec->bv_page, index);
1562 #ifdef CONFIG_ZRAM_MULTI_COMP
1564 * This function will decompress (unless it's ZRAM_HUGE) the page and then
1565 * attempt to compress it using provided compression algorithm priority
1566 * (which is potentially more effective).
1568 * Corresponding ZRAM slot should be locked.
1570 static int zram_recompress(struct zram *zram, u32 index, struct page *page,
1571 u32 threshold, u32 prio, u32 prio_max)
1573 struct zcomp_strm *zstrm = NULL;
1574 unsigned long handle_old;
1575 unsigned long handle_new;
1576 unsigned int comp_len_old;
1577 unsigned int comp_len_new;
1578 unsigned int class_index_old;
1579 unsigned int class_index_new;
1580 u32 num_recomps = 0;
1584 handle_old = zram_get_handle(zram, index);
1588 comp_len_old = zram_get_obj_size(zram, index);
1590 * Do not recompress objects that are already "small enough".
1592 if (comp_len_old < threshold)
1595 ret = zram_read_from_zspool(zram, page, index);
1599 class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old);
1601 * Iterate the secondary comp algorithms list (in order of priority)
1602 * and try to recompress the page.
1604 for (; prio < prio_max; prio++) {
1605 if (!zram->comps[prio])
1609 * Skip if the object is already re-compressed with a higher
1610 * priority algorithm (or same algorithm).
1612 if (prio <= zram_get_priority(zram, index))
1616 zstrm = zcomp_stream_get(zram->comps[prio]);
1617 src = kmap_local_page(page);
1618 ret = zcomp_compress(zstrm, src, &comp_len_new);
1622 zcomp_stream_put(zram->comps[prio]);
1626 class_index_new = zs_lookup_class_index(zram->mem_pool,
1629 /* Continue until we make progress */
1630 if (class_index_new >= class_index_old ||
1631 (threshold && comp_len_new >= threshold)) {
1632 zcomp_stream_put(zram->comps[prio]);
1636 /* Recompression was successful so break out */
1641 * We did not try to recompress, e.g. when we have only one
1642 * secondary algorithm and the page is already recompressed
1643 * using that algorithm
1648 if (class_index_new >= class_index_old) {
1650 * Secondary algorithms failed to re-compress the page
1651 * in a way that would save memory, mark the object as
1652 * incompressible so that we will not try to compress
1655 * We need to make sure that all secondary algorithms have
1656 * failed, so we test if the number of recompressions matches
1657 * the number of active secondary algorithms.
1659 if (num_recomps == zram->num_active_comps - 1)
1660 zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE);
1664 /* Successful recompression but above threshold */
1665 if (threshold && comp_len_new >= threshold)
1669 * No direct reclaim (slow path) for handle allocation and no
1670 * re-compression attempt (unlike in zram_write_bvec()) since
1671 * we already have stored that object in zsmalloc. If we cannot
1672 * alloc memory for recompressed object then we bail out and
1673 * simply keep the old (existing) object in zsmalloc.
1675 handle_new = zs_malloc(zram->mem_pool, comp_len_new,
1676 __GFP_KSWAPD_RECLAIM |
1680 if (IS_ERR_VALUE(handle_new)) {
1681 zcomp_stream_put(zram->comps[prio]);
1682 return PTR_ERR((void *)handle_new);
1685 dst = zs_map_object(zram->mem_pool, handle_new, ZS_MM_WO);
1686 memcpy(dst, zstrm->buffer, comp_len_new);
1687 zcomp_stream_put(zram->comps[prio]);
1689 zs_unmap_object(zram->mem_pool, handle_new);
1691 zram_free_page(zram, index);
1692 zram_set_handle(zram, index, handle_new);
1693 zram_set_obj_size(zram, index, comp_len_new);
1694 zram_set_priority(zram, index, prio);
1696 atomic64_add(comp_len_new, &zram->stats.compr_data_size);
1697 atomic64_inc(&zram->stats.pages_stored);
1702 #define RECOMPRESS_IDLE (1 << 0)
1703 #define RECOMPRESS_HUGE (1 << 1)
1705 static ssize_t recompress_store(struct device *dev,
1706 struct device_attribute *attr,
1707 const char *buf, size_t len)
1709 u32 prio = ZRAM_SECONDARY_COMP, prio_max = ZRAM_MAX_COMPS;
1710 struct zram *zram = dev_to_zram(dev);
1711 unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
1712 char *args, *param, *val, *algo = NULL;
1713 u32 mode = 0, threshold = 0;
1714 unsigned long index;
1718 args = skip_spaces(buf);
1720 args = next_arg(args, ¶m, &val);
1725 if (!strcmp(param, "type")) {
1726 if (!strcmp(val, "idle"))
1727 mode = RECOMPRESS_IDLE;
1728 if (!strcmp(val, "huge"))
1729 mode = RECOMPRESS_HUGE;
1730 if (!strcmp(val, "huge_idle"))
1731 mode = RECOMPRESS_IDLE | RECOMPRESS_HUGE;
1735 if (!strcmp(param, "threshold")) {
1737 * We will re-compress only idle objects equal or
1738 * greater in size than watermark.
1740 ret = kstrtouint(val, 10, &threshold);
1746 if (!strcmp(param, "algo")) {
1752 if (threshold >= huge_class_size)
1755 down_read(&zram->init_lock);
1756 if (!init_done(zram)) {
1758 goto release_init_lock;
1764 for (; prio < ZRAM_MAX_COMPS; prio++) {
1765 if (!zram->comp_algs[prio])
1768 if (!strcmp(zram->comp_algs[prio], algo)) {
1769 prio_max = min(prio + 1, ZRAM_MAX_COMPS);
1777 goto release_init_lock;
1781 page = alloc_page(GFP_KERNEL);
1784 goto release_init_lock;
1788 for (index = 0; index < nr_pages; index++) {
1791 zram_slot_lock(zram, index);
1793 if (!zram_allocated(zram, index))
1796 if (mode & RECOMPRESS_IDLE &&
1797 !zram_test_flag(zram, index, ZRAM_IDLE))
1800 if (mode & RECOMPRESS_HUGE &&
1801 !zram_test_flag(zram, index, ZRAM_HUGE))
1804 if (zram_test_flag(zram, index, ZRAM_WB) ||
1805 zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
1806 zram_test_flag(zram, index, ZRAM_SAME) ||
1807 zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
1810 err = zram_recompress(zram, index, page, threshold,
1813 zram_slot_unlock(zram, index);
1825 up_read(&zram->init_lock);
1830 static void zram_bio_discard(struct zram *zram, struct bio *bio)
1832 size_t n = bio->bi_iter.bi_size;
1833 u32 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1834 u32 offset = (bio->bi_iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
1838 * zram manages data in physical block size units. Because logical block
1839 * size isn't identical with physical block size on some arch, we
1840 * could get a discard request pointing to a specific offset within a
1841 * certain physical block. Although we can handle this request by
1842 * reading that physiclal block and decompressing and partially zeroing
1843 * and re-compressing and then re-storing it, this isn't reasonable
1844 * because our intent with a discard request is to save memory. So
1845 * skipping this logical block is appropriate here.
1848 if (n <= (PAGE_SIZE - offset))
1851 n -= (PAGE_SIZE - offset);
1855 while (n >= PAGE_SIZE) {
1856 zram_slot_lock(zram, index);
1857 zram_free_page(zram, index);
1858 zram_slot_unlock(zram, index);
1859 atomic64_inc(&zram->stats.notify_free);
1867 static void zram_bio_read(struct zram *zram, struct bio *bio)
1869 unsigned long start_time = bio_start_io_acct(bio);
1870 struct bvec_iter iter = bio->bi_iter;
1873 u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1874 u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
1876 struct bio_vec bv = bio_iter_iovec(bio, iter);
1878 bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
1880 if (zram_bvec_read(zram, &bv, index, offset, bio) < 0) {
1881 atomic64_inc(&zram->stats.failed_reads);
1882 bio->bi_status = BLK_STS_IOERR;
1885 flush_dcache_page(bv.bv_page);
1887 zram_slot_lock(zram, index);
1888 zram_accessed(zram, index);
1889 zram_slot_unlock(zram, index);
1891 bio_advance_iter_single(bio, &iter, bv.bv_len);
1892 } while (iter.bi_size);
1894 bio_end_io_acct(bio, start_time);
1898 static void zram_bio_write(struct zram *zram, struct bio *bio)
1900 unsigned long start_time = bio_start_io_acct(bio);
1901 struct bvec_iter iter = bio->bi_iter;
1904 u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1905 u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
1907 struct bio_vec bv = bio_iter_iovec(bio, iter);
1909 bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
1911 if (zram_bvec_write(zram, &bv, index, offset, bio) < 0) {
1912 atomic64_inc(&zram->stats.failed_writes);
1913 bio->bi_status = BLK_STS_IOERR;
1917 zram_slot_lock(zram, index);
1918 zram_accessed(zram, index);
1919 zram_slot_unlock(zram, index);
1921 bio_advance_iter_single(bio, &iter, bv.bv_len);
1922 } while (iter.bi_size);
1924 bio_end_io_acct(bio, start_time);
1929 * Handler function for all zram I/O requests.
1931 static void zram_submit_bio(struct bio *bio)
1933 struct zram *zram = bio->bi_bdev->bd_disk->private_data;
1935 switch (bio_op(bio)) {
1937 zram_bio_read(zram, bio);
1940 zram_bio_write(zram, bio);
1942 case REQ_OP_DISCARD:
1943 case REQ_OP_WRITE_ZEROES:
1944 zram_bio_discard(zram, bio);
1952 static void zram_slot_free_notify(struct block_device *bdev,
1953 unsigned long index)
1957 zram = bdev->bd_disk->private_data;
1959 atomic64_inc(&zram->stats.notify_free);
1960 if (!zram_slot_trylock(zram, index)) {
1961 atomic64_inc(&zram->stats.miss_free);
1965 zram_free_page(zram, index);
1966 zram_slot_unlock(zram, index);
1969 static void zram_destroy_comps(struct zram *zram)
1973 for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
1974 struct zcomp *comp = zram->comps[prio];
1976 zram->comps[prio] = NULL;
1979 zcomp_destroy(comp);
1980 zram->num_active_comps--;
1984 static void zram_reset_device(struct zram *zram)
1986 down_write(&zram->init_lock);
1988 zram->limit_pages = 0;
1990 if (!init_done(zram)) {
1991 up_write(&zram->init_lock);
1995 set_capacity_and_notify(zram->disk, 0);
1996 part_stat_set_all(zram->disk->part0, 0);
1998 /* I/O operation under all of CPU are done so let's free */
1999 zram_meta_free(zram, zram->disksize);
2001 zram_destroy_comps(zram);
2002 memset(&zram->stats, 0, sizeof(zram->stats));
2005 comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
2006 up_write(&zram->init_lock);
2009 static ssize_t disksize_store(struct device *dev,
2010 struct device_attribute *attr, const char *buf, size_t len)
2014 struct zram *zram = dev_to_zram(dev);
2018 disksize = memparse(buf, NULL);
2022 down_write(&zram->init_lock);
2023 if (init_done(zram)) {
2024 pr_info("Cannot change disksize for initialized device\n");
2029 disksize = PAGE_ALIGN(disksize);
2030 if (!zram_meta_alloc(zram, disksize)) {
2035 for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
2036 if (!zram->comp_algs[prio])
2039 comp = zcomp_create(zram->comp_algs[prio]);
2041 pr_err("Cannot initialise %s compressing backend\n",
2042 zram->comp_algs[prio]);
2043 err = PTR_ERR(comp);
2044 goto out_free_comps;
2047 zram->comps[prio] = comp;
2048 zram->num_active_comps++;
2050 zram->disksize = disksize;
2051 set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT);
2052 up_write(&zram->init_lock);
2057 zram_destroy_comps(zram);
2058 zram_meta_free(zram, disksize);
2060 up_write(&zram->init_lock);
2064 static ssize_t reset_store(struct device *dev,
2065 struct device_attribute *attr, const char *buf, size_t len)
2068 unsigned short do_reset;
2070 struct gendisk *disk;
2072 ret = kstrtou16(buf, 10, &do_reset);
2079 zram = dev_to_zram(dev);
2082 mutex_lock(&disk->open_mutex);
2083 /* Do not reset an active device or claimed device */
2084 if (disk_openers(disk) || zram->claim) {
2085 mutex_unlock(&disk->open_mutex);
2089 /* From now on, anyone can't open /dev/zram[0-9] */
2091 mutex_unlock(&disk->open_mutex);
2093 /* Make sure all the pending I/O are finished */
2094 sync_blockdev(disk->part0);
2095 zram_reset_device(zram);
2097 mutex_lock(&disk->open_mutex);
2098 zram->claim = false;
2099 mutex_unlock(&disk->open_mutex);
2104 static int zram_open(struct gendisk *disk, blk_mode_t mode)
2106 struct zram *zram = disk->private_data;
2108 WARN_ON(!mutex_is_locked(&disk->open_mutex));
2110 /* zram was claimed to reset so open request fails */
2116 static const struct block_device_operations zram_devops = {
2118 .submit_bio = zram_submit_bio,
2119 .swap_slot_free_notify = zram_slot_free_notify,
2120 .owner = THIS_MODULE
2123 static DEVICE_ATTR_WO(compact);
2124 static DEVICE_ATTR_RW(disksize);
2125 static DEVICE_ATTR_RO(initstate);
2126 static DEVICE_ATTR_WO(reset);
2127 static DEVICE_ATTR_WO(mem_limit);
2128 static DEVICE_ATTR_WO(mem_used_max);
2129 static DEVICE_ATTR_WO(idle);
2130 static DEVICE_ATTR_RW(max_comp_streams);
2131 static DEVICE_ATTR_RW(comp_algorithm);
2132 #ifdef CONFIG_ZRAM_WRITEBACK
2133 static DEVICE_ATTR_RW(backing_dev);
2134 static DEVICE_ATTR_WO(writeback);
2135 static DEVICE_ATTR_RW(writeback_limit);
2136 static DEVICE_ATTR_RW(writeback_limit_enable);
2138 #ifdef CONFIG_ZRAM_MULTI_COMP
2139 static DEVICE_ATTR_RW(recomp_algorithm);
2140 static DEVICE_ATTR_WO(recompress);
2143 static struct attribute *zram_disk_attrs[] = {
2144 &dev_attr_disksize.attr,
2145 &dev_attr_initstate.attr,
2146 &dev_attr_reset.attr,
2147 &dev_attr_compact.attr,
2148 &dev_attr_mem_limit.attr,
2149 &dev_attr_mem_used_max.attr,
2150 &dev_attr_idle.attr,
2151 &dev_attr_max_comp_streams.attr,
2152 &dev_attr_comp_algorithm.attr,
2153 #ifdef CONFIG_ZRAM_WRITEBACK
2154 &dev_attr_backing_dev.attr,
2155 &dev_attr_writeback.attr,
2156 &dev_attr_writeback_limit.attr,
2157 &dev_attr_writeback_limit_enable.attr,
2159 &dev_attr_io_stat.attr,
2160 &dev_attr_mm_stat.attr,
2161 #ifdef CONFIG_ZRAM_WRITEBACK
2162 &dev_attr_bd_stat.attr,
2164 &dev_attr_debug_stat.attr,
2165 #ifdef CONFIG_ZRAM_MULTI_COMP
2166 &dev_attr_recomp_algorithm.attr,
2167 &dev_attr_recompress.attr,
2172 ATTRIBUTE_GROUPS(zram_disk);
2175 * Allocate and initialize new zram device. the function returns
2176 * '>= 0' device_id upon success, and negative value otherwise.
2178 static int zram_add(void)
2180 struct queue_limits lim = {
2181 .logical_block_size = ZRAM_LOGICAL_BLOCK_SIZE,
2183 * To ensure that we always get PAGE_SIZE aligned and
2184 * n*PAGE_SIZED sized I/O requests.
2186 .physical_block_size = PAGE_SIZE,
2187 .io_min = PAGE_SIZE,
2188 .io_opt = PAGE_SIZE,
2189 .max_hw_discard_sectors = UINT_MAX,
2191 * zram_bio_discard() will clear all logical blocks if logical
2192 * block size is identical with physical block size(PAGE_SIZE).
2193 * But if it is different, we will skip discarding some parts of
2194 * logical blocks in the part of the request range which isn't
2195 * aligned to physical block size. So we can't ensure that all
2196 * discarded logical blocks are zeroed.
2198 #if ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE
2199 .max_write_zeroes_sectors = UINT_MAX,
2205 zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
2209 ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
2214 init_rwsem(&zram->init_lock);
2215 #ifdef CONFIG_ZRAM_WRITEBACK
2216 spin_lock_init(&zram->wb_limit_lock);
2219 /* gendisk structure */
2220 zram->disk = blk_alloc_disk(&lim, NUMA_NO_NODE);
2221 if (IS_ERR(zram->disk)) {
2222 pr_err("Error allocating disk structure for device %d\n",
2224 ret = PTR_ERR(zram->disk);
2228 zram->disk->major = zram_major;
2229 zram->disk->first_minor = device_id;
2230 zram->disk->minors = 1;
2231 zram->disk->flags |= GENHD_FL_NO_PART;
2232 zram->disk->fops = &zram_devops;
2233 zram->disk->private_data = zram;
2234 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
2236 /* Actual capacity set using sysfs (/sys/block/zram<id>/disksize */
2237 set_capacity(zram->disk, 0);
2238 /* zram devices sort of resembles non-rotational disks */
2239 blk_queue_flag_set(QUEUE_FLAG_NONROT, zram->disk->queue);
2240 blk_queue_flag_set(QUEUE_FLAG_SYNCHRONOUS, zram->disk->queue);
2241 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue);
2242 ret = device_add_disk(NULL, zram->disk, zram_disk_groups);
2244 goto out_cleanup_disk;
2246 comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
2248 zram_debugfs_register(zram);
2249 pr_info("Added device: %s\n", zram->disk->disk_name);
2253 put_disk(zram->disk);
2255 idr_remove(&zram_index_idr, device_id);
2261 static int zram_remove(struct zram *zram)
2265 mutex_lock(&zram->disk->open_mutex);
2266 if (disk_openers(zram->disk)) {
2267 mutex_unlock(&zram->disk->open_mutex);
2271 claimed = zram->claim;
2274 mutex_unlock(&zram->disk->open_mutex);
2276 zram_debugfs_unregister(zram);
2280 * If we were claimed by reset_store(), del_gendisk() will
2281 * wait until reset_store() is done, so nothing need to do.
2285 /* Make sure all the pending I/O are finished */
2286 sync_blockdev(zram->disk->part0);
2287 zram_reset_device(zram);
2290 pr_info("Removed device: %s\n", zram->disk->disk_name);
2292 del_gendisk(zram->disk);
2294 /* del_gendisk drains pending reset_store */
2295 WARN_ON_ONCE(claimed && zram->claim);
2298 * disksize_store() may be called in between zram_reset_device()
2299 * and del_gendisk(), so run the last reset to avoid leaking
2300 * anything allocated with disksize_store()
2302 zram_reset_device(zram);
2304 put_disk(zram->disk);
2309 /* zram-control sysfs attributes */
2312 * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a
2313 * sense that reading from this file does alter the state of your system -- it
2314 * creates a new un-initialized zram device and returns back this device's
2315 * device_id (or an error code if it fails to create a new device).
2317 static ssize_t hot_add_show(const struct class *class,
2318 const struct class_attribute *attr,
2323 mutex_lock(&zram_index_mutex);
2325 mutex_unlock(&zram_index_mutex);
2329 return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
2331 /* This attribute must be set to 0400, so CLASS_ATTR_RO() can not be used */
2332 static struct class_attribute class_attr_hot_add =
2333 __ATTR(hot_add, 0400, hot_add_show, NULL);
2335 static ssize_t hot_remove_store(const struct class *class,
2336 const struct class_attribute *attr,
2343 /* dev_id is gendisk->first_minor, which is `int' */
2344 ret = kstrtoint(buf, 10, &dev_id);
2350 mutex_lock(&zram_index_mutex);
2352 zram = idr_find(&zram_index_idr, dev_id);
2354 ret = zram_remove(zram);
2356 idr_remove(&zram_index_idr, dev_id);
2361 mutex_unlock(&zram_index_mutex);
2362 return ret ? ret : count;
2364 static CLASS_ATTR_WO(hot_remove);
2366 static struct attribute *zram_control_class_attrs[] = {
2367 &class_attr_hot_add.attr,
2368 &class_attr_hot_remove.attr,
2371 ATTRIBUTE_GROUPS(zram_control_class);
2373 static struct class zram_control_class = {
2374 .name = "zram-control",
2375 .class_groups = zram_control_class_groups,
2378 static int zram_remove_cb(int id, void *ptr, void *data)
2380 WARN_ON_ONCE(zram_remove(ptr));
2384 static void destroy_devices(void)
2386 class_unregister(&zram_control_class);
2387 idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
2388 zram_debugfs_destroy();
2389 idr_destroy(&zram_index_idr);
2390 unregister_blkdev(zram_major, "zram");
2391 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2394 static int __init zram_init(void)
2398 BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > BITS_PER_LONG);
2400 ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
2401 zcomp_cpu_up_prepare, zcomp_cpu_dead);
2405 ret = class_register(&zram_control_class);
2407 pr_err("Unable to register zram-control class\n");
2408 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2412 zram_debugfs_create();
2413 zram_major = register_blkdev(0, "zram");
2414 if (zram_major <= 0) {
2415 pr_err("Unable to get major number\n");
2416 class_unregister(&zram_control_class);
2417 cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2421 while (num_devices != 0) {
2422 mutex_lock(&zram_index_mutex);
2424 mutex_unlock(&zram_index_mutex);
2437 static void __exit zram_exit(void)
2442 module_init(zram_init);
2443 module_exit(zram_exit);
2445 module_param(num_devices, uint, 0);
2446 MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
2448 MODULE_LICENSE("Dual BSD/GPL");
2449 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
2450 MODULE_DESCRIPTION("Compressed RAM Block Device");