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 #ifdef CONFIG_ZRAM_DEBUG
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/bio.h>
25 #include <linux/bitops.h>
26 #include <linux/blkdev.h>
27 #include <linux/buffer_head.h>
28 #include <linux/device.h>
29 #include <linux/genhd.h>
30 #include <linux/highmem.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/vmalloc.h>
34 #include <linux/err.h>
39 static int zram_major
;
40 static struct zram
*zram_devices
;
41 static const char *default_compressor
= "lzo";
43 /* Module params (documentation at end) */
44 static unsigned int num_devices
= 1;
46 static inline void deprecated_attr_warn(const char *name
)
48 pr_warn_once("%d (%s) Attribute %s (and others) will be removed. %s\n",
52 "See zram documentation.");
55 #define ZRAM_ATTR_RO(name) \
56 static ssize_t name##_show(struct device *d, \
57 struct device_attribute *attr, char *b) \
59 struct zram *zram = dev_to_zram(d); \
61 deprecated_attr_warn(__stringify(name)); \
62 return scnprintf(b, PAGE_SIZE, "%llu\n", \
63 (u64)atomic64_read(&zram->stats.name)); \
65 static DEVICE_ATTR_RO(name);
67 static inline bool init_done(struct zram
*zram
)
69 return zram
->disksize
;
72 static inline struct zram
*dev_to_zram(struct device
*dev
)
74 return (struct zram
*)dev_to_disk(dev
)->private_data
;
77 static ssize_t
compact_store(struct device
*dev
,
78 struct device_attribute
*attr
, const char *buf
, size_t len
)
80 unsigned long nr_migrated
;
81 struct zram
*zram
= dev_to_zram(dev
);
82 struct zram_meta
*meta
;
84 down_read(&zram
->init_lock
);
85 if (!init_done(zram
)) {
86 up_read(&zram
->init_lock
);
91 nr_migrated
= zs_compact(meta
->mem_pool
);
92 atomic64_add(nr_migrated
, &zram
->stats
.num_migrated
);
93 up_read(&zram
->init_lock
);
98 static ssize_t
disksize_show(struct device
*dev
,
99 struct device_attribute
*attr
, char *buf
)
101 struct zram
*zram
= dev_to_zram(dev
);
103 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", zram
->disksize
);
106 static ssize_t
initstate_show(struct device
*dev
,
107 struct device_attribute
*attr
, char *buf
)
110 struct zram
*zram
= dev_to_zram(dev
);
112 down_read(&zram
->init_lock
);
113 val
= init_done(zram
);
114 up_read(&zram
->init_lock
);
116 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
119 static ssize_t
orig_data_size_show(struct device
*dev
,
120 struct device_attribute
*attr
, char *buf
)
122 struct zram
*zram
= dev_to_zram(dev
);
124 deprecated_attr_warn("orig_data_size");
125 return scnprintf(buf
, PAGE_SIZE
, "%llu\n",
126 (u64
)(atomic64_read(&zram
->stats
.pages_stored
)) << PAGE_SHIFT
);
129 static ssize_t
mem_used_total_show(struct device
*dev
,
130 struct device_attribute
*attr
, char *buf
)
133 struct zram
*zram
= dev_to_zram(dev
);
135 deprecated_attr_warn("mem_used_total");
136 down_read(&zram
->init_lock
);
137 if (init_done(zram
)) {
138 struct zram_meta
*meta
= zram
->meta
;
139 val
= zs_get_total_pages(meta
->mem_pool
);
141 up_read(&zram
->init_lock
);
143 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", val
<< PAGE_SHIFT
);
146 static ssize_t
max_comp_streams_show(struct device
*dev
,
147 struct device_attribute
*attr
, char *buf
)
150 struct zram
*zram
= dev_to_zram(dev
);
152 down_read(&zram
->init_lock
);
153 val
= zram
->max_comp_streams
;
154 up_read(&zram
->init_lock
);
156 return scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
159 static ssize_t
mem_limit_show(struct device
*dev
,
160 struct device_attribute
*attr
, char *buf
)
163 struct zram
*zram
= dev_to_zram(dev
);
165 deprecated_attr_warn("mem_limit");
166 down_read(&zram
->init_lock
);
167 val
= zram
->limit_pages
;
168 up_read(&zram
->init_lock
);
170 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", val
<< PAGE_SHIFT
);
173 static ssize_t
mem_limit_store(struct device
*dev
,
174 struct device_attribute
*attr
, const char *buf
, size_t len
)
178 struct zram
*zram
= dev_to_zram(dev
);
180 limit
= memparse(buf
, &tmp
);
181 if (buf
== tmp
) /* no chars parsed, invalid input */
184 down_write(&zram
->init_lock
);
185 zram
->limit_pages
= PAGE_ALIGN(limit
) >> PAGE_SHIFT
;
186 up_write(&zram
->init_lock
);
191 static ssize_t
mem_used_max_show(struct device
*dev
,
192 struct device_attribute
*attr
, char *buf
)
195 struct zram
*zram
= dev_to_zram(dev
);
197 deprecated_attr_warn("mem_used_max");
198 down_read(&zram
->init_lock
);
200 val
= atomic_long_read(&zram
->stats
.max_used_pages
);
201 up_read(&zram
->init_lock
);
203 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", val
<< PAGE_SHIFT
);
206 static ssize_t
mem_used_max_store(struct device
*dev
,
207 struct device_attribute
*attr
, const char *buf
, size_t len
)
211 struct zram
*zram
= dev_to_zram(dev
);
213 err
= kstrtoul(buf
, 10, &val
);
217 down_read(&zram
->init_lock
);
218 if (init_done(zram
)) {
219 struct zram_meta
*meta
= zram
->meta
;
220 atomic_long_set(&zram
->stats
.max_used_pages
,
221 zs_get_total_pages(meta
->mem_pool
));
223 up_read(&zram
->init_lock
);
228 static ssize_t
max_comp_streams_store(struct device
*dev
,
229 struct device_attribute
*attr
, const char *buf
, size_t len
)
232 struct zram
*zram
= dev_to_zram(dev
);
235 ret
= kstrtoint(buf
, 0, &num
);
241 down_write(&zram
->init_lock
);
242 if (init_done(zram
)) {
243 if (!zcomp_set_max_streams(zram
->comp
, num
)) {
244 pr_info("Cannot change max compression streams\n");
250 zram
->max_comp_streams
= num
;
253 up_write(&zram
->init_lock
);
257 static ssize_t
comp_algorithm_show(struct device
*dev
,
258 struct device_attribute
*attr
, char *buf
)
261 struct zram
*zram
= dev_to_zram(dev
);
263 down_read(&zram
->init_lock
);
264 sz
= zcomp_available_show(zram
->compressor
, buf
);
265 up_read(&zram
->init_lock
);
270 static ssize_t
comp_algorithm_store(struct device
*dev
,
271 struct device_attribute
*attr
, const char *buf
, size_t len
)
273 struct zram
*zram
= dev_to_zram(dev
);
274 down_write(&zram
->init_lock
);
275 if (init_done(zram
)) {
276 up_write(&zram
->init_lock
);
277 pr_info("Can't change algorithm for initialized device\n");
280 strlcpy(zram
->compressor
, buf
, sizeof(zram
->compressor
));
281 up_write(&zram
->init_lock
);
285 /* flag operations needs meta->tb_lock */
286 static int zram_test_flag(struct zram_meta
*meta
, u32 index
,
287 enum zram_pageflags flag
)
289 return meta
->table
[index
].value
& BIT(flag
);
292 static void zram_set_flag(struct zram_meta
*meta
, u32 index
,
293 enum zram_pageflags flag
)
295 meta
->table
[index
].value
|= BIT(flag
);
298 static void zram_clear_flag(struct zram_meta
*meta
, u32 index
,
299 enum zram_pageflags flag
)
301 meta
->table
[index
].value
&= ~BIT(flag
);
304 static size_t zram_get_obj_size(struct zram_meta
*meta
, u32 index
)
306 return meta
->table
[index
].value
& (BIT(ZRAM_FLAG_SHIFT
) - 1);
309 static void zram_set_obj_size(struct zram_meta
*meta
,
310 u32 index
, size_t size
)
312 unsigned long flags
= meta
->table
[index
].value
>> ZRAM_FLAG_SHIFT
;
314 meta
->table
[index
].value
= (flags
<< ZRAM_FLAG_SHIFT
) | size
;
317 static inline int is_partial_io(struct bio_vec
*bvec
)
319 return bvec
->bv_len
!= PAGE_SIZE
;
323 * Check if request is within bounds and aligned on zram logical blocks.
325 static inline int valid_io_request(struct zram
*zram
,
326 sector_t start
, unsigned int size
)
330 /* unaligned request */
331 if (unlikely(start
& (ZRAM_SECTOR_PER_LOGICAL_BLOCK
- 1)))
333 if (unlikely(size
& (ZRAM_LOGICAL_BLOCK_SIZE
- 1)))
336 end
= start
+ (size
>> SECTOR_SHIFT
);
337 bound
= zram
->disksize
>> SECTOR_SHIFT
;
338 /* out of range range */
339 if (unlikely(start
>= bound
|| end
> bound
|| start
> end
))
342 /* I/O request is valid */
346 static void zram_meta_free(struct zram_meta
*meta
, u64 disksize
)
348 size_t num_pages
= disksize
>> PAGE_SHIFT
;
351 /* Free all pages that are still in this zram device */
352 for (index
= 0; index
< num_pages
; index
++) {
353 unsigned long handle
= meta
->table
[index
].handle
;
358 zs_free(meta
->mem_pool
, handle
);
361 zs_destroy_pool(meta
->mem_pool
);
366 static struct zram_meta
*zram_meta_alloc(int device_id
, u64 disksize
)
370 struct zram_meta
*meta
= kmalloc(sizeof(*meta
), GFP_KERNEL
);
375 num_pages
= disksize
>> PAGE_SHIFT
;
376 meta
->table
= vzalloc(num_pages
* sizeof(*meta
->table
));
378 pr_err("Error allocating zram address table\n");
382 snprintf(pool_name
, sizeof(pool_name
), "zram%d", device_id
);
383 meta
->mem_pool
= zs_create_pool(pool_name
, GFP_NOIO
| __GFP_HIGHMEM
);
384 if (!meta
->mem_pool
) {
385 pr_err("Error creating memory pool\n");
397 static inline bool zram_meta_get(struct zram
*zram
)
399 if (atomic_inc_not_zero(&zram
->refcount
))
404 static inline void zram_meta_put(struct zram
*zram
)
406 atomic_dec(&zram
->refcount
);
409 static void update_position(u32
*index
, int *offset
, struct bio_vec
*bvec
)
411 if (*offset
+ bvec
->bv_len
>= PAGE_SIZE
)
413 *offset
= (*offset
+ bvec
->bv_len
) % PAGE_SIZE
;
416 static int page_zero_filled(void *ptr
)
421 page
= (unsigned long *)ptr
;
423 for (pos
= 0; pos
!= PAGE_SIZE
/ sizeof(*page
); pos
++) {
431 static void handle_zero_page(struct bio_vec
*bvec
)
433 struct page
*page
= bvec
->bv_page
;
436 user_mem
= kmap_atomic(page
);
437 if (is_partial_io(bvec
))
438 memset(user_mem
+ bvec
->bv_offset
, 0, bvec
->bv_len
);
440 clear_page(user_mem
);
441 kunmap_atomic(user_mem
);
443 flush_dcache_page(page
);
448 * To protect concurrent access to the same index entry,
449 * caller should hold this table index entry's bit_spinlock to
450 * indicate this index entry is accessing.
452 static void zram_free_page(struct zram
*zram
, size_t index
)
454 struct zram_meta
*meta
= zram
->meta
;
455 unsigned long handle
= meta
->table
[index
].handle
;
457 if (unlikely(!handle
)) {
459 * No memory is allocated for zero filled pages.
460 * Simply clear zero page flag.
462 if (zram_test_flag(meta
, index
, ZRAM_ZERO
)) {
463 zram_clear_flag(meta
, index
, ZRAM_ZERO
);
464 atomic64_dec(&zram
->stats
.zero_pages
);
469 zs_free(meta
->mem_pool
, handle
);
471 atomic64_sub(zram_get_obj_size(meta
, index
),
472 &zram
->stats
.compr_data_size
);
473 atomic64_dec(&zram
->stats
.pages_stored
);
475 meta
->table
[index
].handle
= 0;
476 zram_set_obj_size(meta
, index
, 0);
479 static int zram_decompress_page(struct zram
*zram
, char *mem
, u32 index
)
483 struct zram_meta
*meta
= zram
->meta
;
484 unsigned long handle
;
487 bit_spin_lock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
488 handle
= meta
->table
[index
].handle
;
489 size
= zram_get_obj_size(meta
, index
);
491 if (!handle
|| zram_test_flag(meta
, index
, ZRAM_ZERO
)) {
492 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
497 cmem
= zs_map_object(meta
->mem_pool
, handle
, ZS_MM_RO
);
498 if (size
== PAGE_SIZE
)
499 copy_page(mem
, cmem
);
501 ret
= zcomp_decompress(zram
->comp
, cmem
, size
, mem
);
502 zs_unmap_object(meta
->mem_pool
, handle
);
503 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
505 /* Should NEVER happen. Return bio error if it does. */
507 pr_err("Decompression failed! err=%d, page=%u\n", ret
, index
);
514 static int zram_bvec_read(struct zram
*zram
, struct bio_vec
*bvec
,
515 u32 index
, int offset
)
519 unsigned char *user_mem
, *uncmem
= NULL
;
520 struct zram_meta
*meta
= zram
->meta
;
521 page
= bvec
->bv_page
;
523 bit_spin_lock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
524 if (unlikely(!meta
->table
[index
].handle
) ||
525 zram_test_flag(meta
, index
, ZRAM_ZERO
)) {
526 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
527 handle_zero_page(bvec
);
530 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
532 if (is_partial_io(bvec
))
533 /* Use a temporary buffer to decompress the page */
534 uncmem
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
536 user_mem
= kmap_atomic(page
);
537 if (!is_partial_io(bvec
))
541 pr_info("Unable to allocate temp memory\n");
546 ret
= zram_decompress_page(zram
, uncmem
, index
);
547 /* Should NEVER happen. Return bio error if it does. */
551 if (is_partial_io(bvec
))
552 memcpy(user_mem
+ bvec
->bv_offset
, uncmem
+ offset
,
555 flush_dcache_page(page
);
558 kunmap_atomic(user_mem
);
559 if (is_partial_io(bvec
))
564 static inline void update_used_max(struct zram
*zram
,
565 const unsigned long pages
)
567 unsigned long old_max
, cur_max
;
569 old_max
= atomic_long_read(&zram
->stats
.max_used_pages
);
574 old_max
= atomic_long_cmpxchg(
575 &zram
->stats
.max_used_pages
, cur_max
, pages
);
576 } while (old_max
!= cur_max
);
579 static int zram_bvec_write(struct zram
*zram
, struct bio_vec
*bvec
, u32 index
,
584 unsigned long handle
;
586 unsigned char *user_mem
, *cmem
, *src
, *uncmem
= NULL
;
587 struct zram_meta
*meta
= zram
->meta
;
588 struct zcomp_strm
*zstrm
;
590 unsigned long alloced_pages
;
592 page
= bvec
->bv_page
;
593 if (is_partial_io(bvec
)) {
595 * This is a partial IO. We need to read the full page
596 * before to write the changes.
598 uncmem
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
603 ret
= zram_decompress_page(zram
, uncmem
, index
);
608 zstrm
= zcomp_strm_find(zram
->comp
);
610 user_mem
= kmap_atomic(page
);
612 if (is_partial_io(bvec
)) {
613 memcpy(uncmem
+ offset
, user_mem
+ bvec
->bv_offset
,
615 kunmap_atomic(user_mem
);
621 if (page_zero_filled(uncmem
)) {
623 kunmap_atomic(user_mem
);
624 /* Free memory associated with this sector now. */
625 bit_spin_lock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
626 zram_free_page(zram
, index
);
627 zram_set_flag(meta
, index
, ZRAM_ZERO
);
628 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
630 atomic64_inc(&zram
->stats
.zero_pages
);
635 ret
= zcomp_compress(zram
->comp
, zstrm
, uncmem
, &clen
);
636 if (!is_partial_io(bvec
)) {
637 kunmap_atomic(user_mem
);
643 pr_err("Compression failed! err=%d\n", ret
);
647 if (unlikely(clen
> max_zpage_size
)) {
649 if (is_partial_io(bvec
))
653 handle
= zs_malloc(meta
->mem_pool
, clen
);
655 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
661 alloced_pages
= zs_get_total_pages(meta
->mem_pool
);
662 if (zram
->limit_pages
&& alloced_pages
> zram
->limit_pages
) {
663 zs_free(meta
->mem_pool
, handle
);
668 update_used_max(zram
, alloced_pages
);
670 cmem
= zs_map_object(meta
->mem_pool
, handle
, ZS_MM_WO
);
672 if ((clen
== PAGE_SIZE
) && !is_partial_io(bvec
)) {
673 src
= kmap_atomic(page
);
674 copy_page(cmem
, src
);
677 memcpy(cmem
, src
, clen
);
680 zcomp_strm_release(zram
->comp
, zstrm
);
682 zs_unmap_object(meta
->mem_pool
, handle
);
685 * Free memory associated with this sector
686 * before overwriting unused sectors.
688 bit_spin_lock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
689 zram_free_page(zram
, index
);
691 meta
->table
[index
].handle
= handle
;
692 zram_set_obj_size(meta
, index
, clen
);
693 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
696 atomic64_add(clen
, &zram
->stats
.compr_data_size
);
697 atomic64_inc(&zram
->stats
.pages_stored
);
700 zcomp_strm_release(zram
->comp
, zstrm
);
701 if (is_partial_io(bvec
))
706 static int zram_bvec_rw(struct zram
*zram
, struct bio_vec
*bvec
, u32 index
,
709 unsigned long start_time
= jiffies
;
712 generic_start_io_acct(rw
, bvec
->bv_len
>> SECTOR_SHIFT
,
716 atomic64_inc(&zram
->stats
.num_reads
);
717 ret
= zram_bvec_read(zram
, bvec
, index
, offset
);
719 atomic64_inc(&zram
->stats
.num_writes
);
720 ret
= zram_bvec_write(zram
, bvec
, index
, offset
);
723 generic_end_io_acct(rw
, &zram
->disk
->part0
, start_time
);
727 atomic64_inc(&zram
->stats
.failed_reads
);
729 atomic64_inc(&zram
->stats
.failed_writes
);
736 * zram_bio_discard - handler on discard request
737 * @index: physical block index in PAGE_SIZE units
738 * @offset: byte offset within physical block
740 static void zram_bio_discard(struct zram
*zram
, u32 index
,
741 int offset
, struct bio
*bio
)
743 size_t n
= bio
->bi_iter
.bi_size
;
744 struct zram_meta
*meta
= zram
->meta
;
747 * zram manages data in physical block size units. Because logical block
748 * size isn't identical with physical block size on some arch, we
749 * could get a discard request pointing to a specific offset within a
750 * certain physical block. Although we can handle this request by
751 * reading that physiclal block and decompressing and partially zeroing
752 * and re-compressing and then re-storing it, this isn't reasonable
753 * because our intent with a discard request is to save memory. So
754 * skipping this logical block is appropriate here.
757 if (n
<= (PAGE_SIZE
- offset
))
760 n
-= (PAGE_SIZE
- offset
);
764 while (n
>= PAGE_SIZE
) {
765 bit_spin_lock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
766 zram_free_page(zram
, index
);
767 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
768 atomic64_inc(&zram
->stats
.notify_free
);
774 static void zram_reset_device(struct zram
*zram
)
776 struct zram_meta
*meta
;
780 down_write(&zram
->init_lock
);
782 zram
->limit_pages
= 0;
784 if (!init_done(zram
)) {
785 up_write(&zram
->init_lock
);
791 disksize
= zram
->disksize
;
793 * Refcount will go down to 0 eventually and r/w handler
794 * cannot handle further I/O so it will bail out by
795 * check zram_meta_get.
799 * We want to free zram_meta in process context to avoid
800 * deadlock between reclaim path and any other locks.
802 wait_event(zram
->io_done
, atomic_read(&zram
->refcount
) == 0);
805 memset(&zram
->stats
, 0, sizeof(zram
->stats
));
807 zram
->max_comp_streams
= 1;
809 set_capacity(zram
->disk
, 0);
810 part_stat_set_all(&zram
->disk
->part0
, 0);
812 up_write(&zram
->init_lock
);
813 /* I/O operation under all of CPU are done so let's free */
814 zram_meta_free(meta
, disksize
);
818 static ssize_t
disksize_store(struct device
*dev
,
819 struct device_attribute
*attr
, const char *buf
, size_t len
)
823 struct zram_meta
*meta
;
824 struct zram
*zram
= dev_to_zram(dev
);
827 disksize
= memparse(buf
, NULL
);
831 disksize
= PAGE_ALIGN(disksize
);
832 meta
= zram_meta_alloc(zram
->disk
->first_minor
, disksize
);
836 comp
= zcomp_create(zram
->compressor
, zram
->max_comp_streams
);
838 pr_info("Cannot initialise %s compressing backend\n",
844 down_write(&zram
->init_lock
);
845 if (init_done(zram
)) {
846 pr_info("Cannot change disksize for initialized device\n");
848 goto out_destroy_comp
;
851 init_waitqueue_head(&zram
->io_done
);
852 atomic_set(&zram
->refcount
, 1);
855 zram
->disksize
= disksize
;
856 set_capacity(zram
->disk
, zram
->disksize
>> SECTOR_SHIFT
);
857 up_write(&zram
->init_lock
);
860 * Revalidate disk out of the init_lock to avoid lockdep splat.
861 * It's okay because disk's capacity is protected by init_lock
862 * so that revalidate_disk always sees up-to-date capacity.
864 revalidate_disk(zram
->disk
);
869 up_write(&zram
->init_lock
);
872 zram_meta_free(meta
, disksize
);
876 static ssize_t
reset_store(struct device
*dev
,
877 struct device_attribute
*attr
, const char *buf
, size_t len
)
880 unsigned short do_reset
;
882 struct block_device
*bdev
;
884 zram
= dev_to_zram(dev
);
885 bdev
= bdget_disk(zram
->disk
, 0);
890 mutex_lock(&bdev
->bd_mutex
);
891 /* Do not reset an active device! */
892 if (bdev
->bd_openers
) {
897 ret
= kstrtou16(buf
, 10, &do_reset
);
906 /* Make sure all pending I/O is finished */
908 zram_reset_device(zram
);
910 mutex_unlock(&bdev
->bd_mutex
);
911 revalidate_disk(zram
->disk
);
917 mutex_unlock(&bdev
->bd_mutex
);
922 static void __zram_make_request(struct zram
*zram
, struct bio
*bio
)
927 struct bvec_iter iter
;
929 index
= bio
->bi_iter
.bi_sector
>> SECTORS_PER_PAGE_SHIFT
;
930 offset
= (bio
->bi_iter
.bi_sector
&
931 (SECTORS_PER_PAGE
- 1)) << SECTOR_SHIFT
;
933 if (unlikely(bio
->bi_rw
& REQ_DISCARD
)) {
934 zram_bio_discard(zram
, index
, offset
, bio
);
939 rw
= bio_data_dir(bio
);
940 bio_for_each_segment(bvec
, bio
, iter
) {
941 int max_transfer_size
= PAGE_SIZE
- offset
;
943 if (bvec
.bv_len
> max_transfer_size
) {
945 * zram_bvec_rw() can only make operation on a single
946 * zram page. Split the bio vector.
950 bv
.bv_page
= bvec
.bv_page
;
951 bv
.bv_len
= max_transfer_size
;
952 bv
.bv_offset
= bvec
.bv_offset
;
954 if (zram_bvec_rw(zram
, &bv
, index
, offset
, rw
) < 0)
957 bv
.bv_len
= bvec
.bv_len
- max_transfer_size
;
958 bv
.bv_offset
+= max_transfer_size
;
959 if (zram_bvec_rw(zram
, &bv
, index
+ 1, 0, rw
) < 0)
962 if (zram_bvec_rw(zram
, &bvec
, index
, offset
, rw
) < 0)
965 update_position(&index
, &offset
, &bvec
);
968 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
977 * Handler function for all zram I/O requests.
979 static void zram_make_request(struct request_queue
*queue
, struct bio
*bio
)
981 struct zram
*zram
= queue
->queuedata
;
983 if (unlikely(!zram_meta_get(zram
)))
986 if (!valid_io_request(zram
, bio
->bi_iter
.bi_sector
,
987 bio
->bi_iter
.bi_size
)) {
988 atomic64_inc(&zram
->stats
.invalid_io
);
992 __zram_make_request(zram
, bio
);
1001 static void zram_slot_free_notify(struct block_device
*bdev
,
1002 unsigned long index
)
1005 struct zram_meta
*meta
;
1007 zram
= bdev
->bd_disk
->private_data
;
1010 bit_spin_lock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
1011 zram_free_page(zram
, index
);
1012 bit_spin_unlock(ZRAM_ACCESS
, &meta
->table
[index
].value
);
1013 atomic64_inc(&zram
->stats
.notify_free
);
1016 static int zram_rw_page(struct block_device
*bdev
, sector_t sector
,
1017 struct page
*page
, int rw
)
1019 int offset
, err
= -EIO
;
1024 zram
= bdev
->bd_disk
->private_data
;
1025 if (unlikely(!zram_meta_get(zram
)))
1028 if (!valid_io_request(zram
, sector
, PAGE_SIZE
)) {
1029 atomic64_inc(&zram
->stats
.invalid_io
);
1034 index
= sector
>> SECTORS_PER_PAGE_SHIFT
;
1035 offset
= sector
& (SECTORS_PER_PAGE
- 1) << SECTOR_SHIFT
;
1038 bv
.bv_len
= PAGE_SIZE
;
1041 err
= zram_bvec_rw(zram
, &bv
, index
, offset
, rw
);
1043 zram_meta_put(zram
);
1046 * If I/O fails, just return error(ie, non-zero) without
1047 * calling page_endio.
1048 * It causes resubmit the I/O with bio request by upper functions
1049 * of rw_page(e.g., swap_readpage, __swap_writepage) and
1050 * bio->bi_end_io does things to handle the error
1051 * (e.g., SetPageError, set_page_dirty and extra works).
1054 page_endio(page
, rw
, 0);
1058 static const struct block_device_operations zram_devops
= {
1059 .swap_slot_free_notify
= zram_slot_free_notify
,
1060 .rw_page
= zram_rw_page
,
1061 .owner
= THIS_MODULE
1064 static DEVICE_ATTR_WO(compact
);
1065 static DEVICE_ATTR_RW(disksize
);
1066 static DEVICE_ATTR_RO(initstate
);
1067 static DEVICE_ATTR_WO(reset
);
1068 static DEVICE_ATTR_RO(orig_data_size
);
1069 static DEVICE_ATTR_RO(mem_used_total
);
1070 static DEVICE_ATTR_RW(mem_limit
);
1071 static DEVICE_ATTR_RW(mem_used_max
);
1072 static DEVICE_ATTR_RW(max_comp_streams
);
1073 static DEVICE_ATTR_RW(comp_algorithm
);
1075 static ssize_t
io_stat_show(struct device
*dev
,
1076 struct device_attribute
*attr
, char *buf
)
1078 struct zram
*zram
= dev_to_zram(dev
);
1081 down_read(&zram
->init_lock
);
1082 ret
= scnprintf(buf
, PAGE_SIZE
,
1083 "%8llu %8llu %8llu %8llu\n",
1084 (u64
)atomic64_read(&zram
->stats
.failed_reads
),
1085 (u64
)atomic64_read(&zram
->stats
.failed_writes
),
1086 (u64
)atomic64_read(&zram
->stats
.invalid_io
),
1087 (u64
)atomic64_read(&zram
->stats
.notify_free
));
1088 up_read(&zram
->init_lock
);
1093 static ssize_t
mm_stat_show(struct device
*dev
,
1094 struct device_attribute
*attr
, char *buf
)
1096 struct zram
*zram
= dev_to_zram(dev
);
1097 u64 orig_size
, mem_used
= 0;
1101 down_read(&zram
->init_lock
);
1102 if (init_done(zram
))
1103 mem_used
= zs_get_total_pages(zram
->meta
->mem_pool
);
1105 orig_size
= atomic64_read(&zram
->stats
.pages_stored
);
1106 max_used
= atomic_long_read(&zram
->stats
.max_used_pages
);
1108 ret
= scnprintf(buf
, PAGE_SIZE
,
1109 "%8llu %8llu %8llu %8lu %8ld %8llu %8llu\n",
1110 orig_size
<< PAGE_SHIFT
,
1111 (u64
)atomic64_read(&zram
->stats
.compr_data_size
),
1112 mem_used
<< PAGE_SHIFT
,
1113 zram
->limit_pages
<< PAGE_SHIFT
,
1114 max_used
<< PAGE_SHIFT
,
1115 (u64
)atomic64_read(&zram
->stats
.zero_pages
),
1116 (u64
)atomic64_read(&zram
->stats
.num_migrated
));
1117 up_read(&zram
->init_lock
);
1122 static DEVICE_ATTR_RO(io_stat
);
1123 static DEVICE_ATTR_RO(mm_stat
);
1124 ZRAM_ATTR_RO(num_reads
);
1125 ZRAM_ATTR_RO(num_writes
);
1126 ZRAM_ATTR_RO(failed_reads
);
1127 ZRAM_ATTR_RO(failed_writes
);
1128 ZRAM_ATTR_RO(invalid_io
);
1129 ZRAM_ATTR_RO(notify_free
);
1130 ZRAM_ATTR_RO(zero_pages
);
1131 ZRAM_ATTR_RO(compr_data_size
);
1133 static struct attribute
*zram_disk_attrs
[] = {
1134 &dev_attr_disksize
.attr
,
1135 &dev_attr_initstate
.attr
,
1136 &dev_attr_reset
.attr
,
1137 &dev_attr_num_reads
.attr
,
1138 &dev_attr_num_writes
.attr
,
1139 &dev_attr_failed_reads
.attr
,
1140 &dev_attr_failed_writes
.attr
,
1141 &dev_attr_compact
.attr
,
1142 &dev_attr_invalid_io
.attr
,
1143 &dev_attr_notify_free
.attr
,
1144 &dev_attr_zero_pages
.attr
,
1145 &dev_attr_orig_data_size
.attr
,
1146 &dev_attr_compr_data_size
.attr
,
1147 &dev_attr_mem_used_total
.attr
,
1148 &dev_attr_mem_limit
.attr
,
1149 &dev_attr_mem_used_max
.attr
,
1150 &dev_attr_max_comp_streams
.attr
,
1151 &dev_attr_comp_algorithm
.attr
,
1152 &dev_attr_io_stat
.attr
,
1153 &dev_attr_mm_stat
.attr
,
1157 static struct attribute_group zram_disk_attr_group
= {
1158 .attrs
= zram_disk_attrs
,
1161 static int create_device(struct zram
*zram
, int device_id
)
1163 struct request_queue
*queue
;
1166 init_rwsem(&zram
->init_lock
);
1168 queue
= blk_alloc_queue(GFP_KERNEL
);
1170 pr_err("Error allocating disk queue for device %d\n",
1175 blk_queue_make_request(queue
, zram_make_request
);
1177 /* gendisk structure */
1178 zram
->disk
= alloc_disk(1);
1180 pr_warn("Error allocating disk structure for device %d\n",
1183 goto out_free_queue
;
1186 zram
->disk
->major
= zram_major
;
1187 zram
->disk
->first_minor
= device_id
;
1188 zram
->disk
->fops
= &zram_devops
;
1189 zram
->disk
->queue
= queue
;
1190 zram
->disk
->queue
->queuedata
= zram
;
1191 zram
->disk
->private_data
= zram
;
1192 snprintf(zram
->disk
->disk_name
, 16, "zram%d", device_id
);
1194 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
1195 set_capacity(zram
->disk
, 0);
1196 /* zram devices sort of resembles non-rotational disks */
1197 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zram
->disk
->queue
);
1198 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, zram
->disk
->queue
);
1200 * To ensure that we always get PAGE_SIZE aligned
1201 * and n*PAGE_SIZED sized I/O requests.
1203 blk_queue_physical_block_size(zram
->disk
->queue
, PAGE_SIZE
);
1204 blk_queue_logical_block_size(zram
->disk
->queue
,
1205 ZRAM_LOGICAL_BLOCK_SIZE
);
1206 blk_queue_io_min(zram
->disk
->queue
, PAGE_SIZE
);
1207 blk_queue_io_opt(zram
->disk
->queue
, PAGE_SIZE
);
1208 zram
->disk
->queue
->limits
.discard_granularity
= PAGE_SIZE
;
1209 zram
->disk
->queue
->limits
.max_discard_sectors
= UINT_MAX
;
1211 * zram_bio_discard() will clear all logical blocks if logical block
1212 * size is identical with physical block size(PAGE_SIZE). But if it is
1213 * different, we will skip discarding some parts of logical blocks in
1214 * the part of the request range which isn't aligned to physical block
1215 * size. So we can't ensure that all discarded logical blocks are
1218 if (ZRAM_LOGICAL_BLOCK_SIZE
== PAGE_SIZE
)
1219 zram
->disk
->queue
->limits
.discard_zeroes_data
= 1;
1221 zram
->disk
->queue
->limits
.discard_zeroes_data
= 0;
1222 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, zram
->disk
->queue
);
1224 add_disk(zram
->disk
);
1226 ret
= sysfs_create_group(&disk_to_dev(zram
->disk
)->kobj
,
1227 &zram_disk_attr_group
);
1229 pr_warn("Error creating sysfs group");
1232 strlcpy(zram
->compressor
, default_compressor
, sizeof(zram
->compressor
));
1234 zram
->max_comp_streams
= 1;
1238 del_gendisk(zram
->disk
);
1239 put_disk(zram
->disk
);
1241 blk_cleanup_queue(queue
);
1246 static void destroy_devices(unsigned int nr
)
1251 for (i
= 0; i
< nr
; i
++) {
1252 zram
= &zram_devices
[i
];
1254 * Remove sysfs first, so no one will perform a disksize
1255 * store while we destroy the devices
1257 sysfs_remove_group(&disk_to_dev(zram
->disk
)->kobj
,
1258 &zram_disk_attr_group
);
1260 zram_reset_device(zram
);
1262 blk_cleanup_queue(zram
->disk
->queue
);
1263 del_gendisk(zram
->disk
);
1264 put_disk(zram
->disk
);
1267 kfree(zram_devices
);
1268 unregister_blkdev(zram_major
, "zram");
1269 pr_info("Destroyed %u device(s)\n", nr
);
1272 static int __init
zram_init(void)
1276 if (num_devices
> max_num_devices
) {
1277 pr_warn("Invalid value for num_devices: %u\n",
1282 zram_major
= register_blkdev(0, "zram");
1283 if (zram_major
<= 0) {
1284 pr_warn("Unable to get major number\n");
1288 /* Allocate the device array and initialize each one */
1289 zram_devices
= kzalloc(num_devices
* sizeof(struct zram
), GFP_KERNEL
);
1290 if (!zram_devices
) {
1291 unregister_blkdev(zram_major
, "zram");
1295 for (dev_id
= 0; dev_id
< num_devices
; dev_id
++) {
1296 ret
= create_device(&zram_devices
[dev_id
], dev_id
);
1301 pr_info("Created %u device(s)\n", num_devices
);
1305 destroy_devices(dev_id
);
1309 static void __exit
zram_exit(void)
1311 destroy_devices(num_devices
);
1314 module_init(zram_init
);
1315 module_exit(zram_exit
);
1317 module_param(num_devices
, uint
, 0);
1318 MODULE_PARM_DESC(num_devices
, "Number of zram devices");
1320 MODULE_LICENSE("Dual BSD/GPL");
1321 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
1322 MODULE_DESCRIPTION("Compressed RAM Block Device");