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
18 #include <linux/rwsem.h>
19 #include <linux/zsmalloc.h>
20 #include <linux/crypto.h>
24 /*-- Configurable parameters */
27 * Pages that compress to size greater than this are stored
28 * uncompressed in memory.
30 static const size_t max_zpage_size
= PAGE_SIZE
/ 4 * 3;
33 * NOTE: max_zpage_size must be less than or equal to:
34 * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
35 * always return failure.
38 /*-- End of configurable params */
40 #define SECTOR_SHIFT 9
41 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
42 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
43 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
44 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
45 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
46 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
50 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
51 * object size (excluding header), the higher bits is for
54 * zram is mainly used for memory efficiency so we want to keep memory
55 * footprint small so we can squeeze size and flags into a field.
56 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
57 * the higher bits is for zram_pageflags.
59 #define ZRAM_FLAG_SHIFT 24
61 /* Flags for zram pages (table[page_no].value) */
63 /* Page consists the same element */
64 ZRAM_SAME
= ZRAM_FLAG_SHIFT
,
65 ZRAM_ACCESS
, /* page is now accessed */
66 ZRAM_WB
, /* page is stored on backing_device */
71 /*-- Data structures */
73 /* Allocated for each disk page */
74 struct zram_table_entry
{
77 unsigned long element
;
83 atomic64_t compr_data_size
; /* compressed size of pages stored */
84 atomic64_t num_reads
; /* failed + successful */
85 atomic64_t num_writes
; /* --do-- */
86 atomic64_t failed_reads
; /* can happen when memory is too low */
87 atomic64_t failed_writes
; /* can happen when memory is too low */
88 atomic64_t invalid_io
; /* non-page-aligned I/O requests */
89 atomic64_t notify_free
; /* no. of swap slot free notifications */
90 atomic64_t same_pages
; /* no. of same element filled pages */
91 atomic64_t pages_stored
; /* no. of pages currently stored */
92 atomic_long_t max_used_pages
; /* no. of maximum pages stored */
93 atomic64_t writestall
; /* no. of write slow paths */
97 struct zram_table_entry
*table
;
98 struct zs_pool
*mem_pool
;
100 struct gendisk
*disk
;
101 /* Prevent concurrent execution of device init */
102 struct rw_semaphore init_lock
;
104 * the number of pages zram can consume for storing compressed data
106 unsigned long limit_pages
;
108 struct zram_stats stats
;
110 * This is the limit on amount of *uncompressed* worth of data
111 * we can store in a disk.
113 u64 disksize
; /* bytes */
114 char compressor
[CRYPTO_MAX_ALG_NAME
];
116 * zram is claimed so open request will be failed
118 bool claim
; /* Protected by bdev->bd_mutex */
119 #ifdef CONFIG_ZRAM_WRITEBACK
120 struct file
*backing_dev
;
121 struct block_device
*bdev
;
122 unsigned int old_block_size
;
123 unsigned long *bitmap
;
124 unsigned long nr_pages
;
125 spinlock_t bitmap_lock
;